How to return value from function to call it sequentially each line with new result

Question

Can you help me figure out with two or maybe one problem here, not sure. I'm trying to get string and integer values from several private static string which contains different processes each with single different type output, which I want return to call sequentially as function, for further combining.

First I want to say that I don know, how to create such function which can return values to get desired result, if way below is a wrong, and it would be good to see some useful example for this case, but what I'm trying to do here, gives me two problems:

If I want call b1 then b2 and again b1, I want get unique calculation result from each, but now I got the equal results from each, if I use random number as it is show below, but I suspect in this case the reason of this problem is a wrong use of random inside the private static string, also need to figure out, how to use it proper for such case.

Result looks like this:

23 23 23  

But also I think it is not only one, same and main problem here, because if I return some different calculation result, for example choosing from different string set of different lists separately inside the private static string b1 and b2, result just returns me the same value twice or several times with repeated process in order of sequential calls. I Can't say for sure, but it does not look like the same reason of problem, which can be above. Because it is looks like this:

 77 34 77

Or to make it clearer, if I choose some word by calculation form string for example: "hello world, how are you" and inside each private static string I make different calculation to get single word from this string, or from different no mater, I got equal result or correctly to say same result with call of same process twice, just like repeating of result and not as separate call of it:

  world you world

This sample below show case only with random number, because if last problem is different and has no connection with wrong random number using, reason must be in general approach, and I should go to solve this problem somehow differently.

class Program
{
    static void Main(string[] args)
    {
        string a1 = b1();
        string a2 = b2();
        string a3 = b1();

        string comb = (a1 + a2 + a3);

        Console.WriteLine(comb);
        Console.ReadLine();          
    }

    private static string b1()
    {
        Random random = new Random();
        int ran1 = random.Next(1, 100);
        return ran1;
    }

    private static string b2()
    {
        Random random = new Random();
        int ran2 = random.Next(1, 100);
        return ran2;
    }
}

EDIT 1 :

Example with string return chosen by random number or any other way:

 static void Main(string[] args)
        {
            string a1 = b1(); 
            string a2 = b2(); 
            string a3 = b1(); 

            string comb  = (a1 + a2 + a3);

            Console.WriteLine(comb);
            Console.ReadLine();
        }

 private static string b1()
        {
            Dictionary<int, string> mass1 = new Dictionary<int, string>() 
                            { { 1, "A" }, { 2, "B" }, { 3, "C" }};

            Random random1 = new Random(); 
            int rndCase2 = random1.Next(1, 4); 
            string key1;
            mass1.TryGetValue(rndCase2, out key1);
            return key1;
        }

private static string b2()
        {
            Dictionary<int, string> mass2 = new Dictionary<int, string>() 
                            { { 1, "E" }, { 2, "F" }, { 3, "G" }};

            Random random2 = new Random(); 
            int rndCase2 = random2.Next(1, 4); 
            string key2;
            mass2.TryGetValue(rndCase2, out key2);
            return key2;
        }

Result is:

AEA

EDIT 2: (SOLVED)

 class Program
    {
        private static Random random = new Random();

        static void Main(string[] args)
        {
            Random random = new Random();
            string a1 = b1();
            string a2 = b2();
            string a3 = b1();

            string comb = (a1 + a2 + a3);

            Console.WriteLine(comb);
            Console.ReadLine();
        }

        private static string b1()
        {
            Dictionary<int, string> mass1 = new Dictionary<int, string>()
                        { { 1, "A" }, { 2, "B" }, { 3, "C" }};

            return mass1[random.Next(1, 4)];
        }

        private static string b2()
        {
            Dictionary<int, string> mass2 = new Dictionary<int, string>()
                        { { 1, "E" }, { 2, "F" }, { 3, "G" }};

            return mass2[random.Next(1, 4)];
        }
    }

Show source
| function   | c#   | string   | random   | return-value   2016-11-09 08:11 2 Answers

Answers ( 2 )

  1. 2016-11-09 08:11

    use one Random instance only

    class Program
    {
       static void Main(string[] args)
        {
    
            Random random = new Random();
            string a1 = b1(random);
            string a2 = b2(random);
            string a3 = b1(random);
    
            string comb = (a1 + a2 + a3);
    
            Console.WriteLine(comb);
            Console.ReadLine();
        }
    
    
        private static string b1(Random random)
        {
            int ran1 = random.Next(1, 100);
            return ran1.ToString();
        }
    
        private static string b2(Random random)
        {
            int ran2 = random.Next(1, 100);
            return ran2.ToString();
        }
    }
    

    edit 1

    the same applies for those other b1() and b2() methods of yours:

        private static string b1(Random random)
        {
            Dictionary<int, string> mass1 = new Dictionary<int, string>()
                        { { 1, "A" }, { 2, "B" }, { 3, "C" }};
    
            return mass1[random.Next(1, 4)];
        }
    
        private static string b2(Random random)
        {
            Dictionary<int, string> mass2 = new Dictionary<int, string>()
                        { { 1, "E" }, { 2, "F" }, { 3, "G" }};
    
            return mass2[random.Next(1, 4)];
        }
    

    while Main() stays the same as in my previous edit

  2. 2016-11-09 08:11

    Since Random generator when created as new Random() initializes from the system timer, there's a good chance that you have three identical generators (with identical sequences) in all three calls. Move Random out the methods:

       class Program {
         // simplest, not thread safe
         private static Random random = new Random();
    
         static void Main(string[] args)
         {
            Random random = new Random();
            string a1 = b1();
            string a2 = b2();
            string a3 = b1();
    
            string comb = (a1 + a2 + a3); // <- please, notice "a3" instead of second "a1"
    
            Console.WriteLine(comb);
            Console.ReadLine();
         }
    
         private static string b1()
         {
            return random.Next(1, 100).ToString();
         }
    
         private static string b2()
         {
            return random.Next(1, 100).ToString();
         }
       }
    

    In your edited code, please, follow the same principle: do not create Random instances, but use static one:

        private static string b1()
        {
            Dictionary<int, string> mass1 = new Dictionary<int, string>() 
                            { { 1, "A" }, { 2, "B" }, { 3, "C" }};
    
            return mass1[random.Next(1, 4)];
        }
    
        private static string b2()
        {
            Dictionary<int, string> mass2 = new Dictionary<int, string>() 
                            { { 1, "E" }, { 2, "F" }, { 3, "G" }};
    
            return mass2[random.Next(1, 4)];
        }   
    
◀ Go back