CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2015
    Posts
    1

    No "real" random numbers when using loop

    Hello,

    I am trying to program a random generator for some text, but facing a strange issue. I assumed that a random number will be generated each time the programm is running through the loop. But everytime the loop is repeated the same results (an so the same random numbers have been chosen?) appear. Also if the length of the arrays is identical, the same random number is used.

    Can anyone give me a hint, how to

    1. I achieve that the random values will change for each instance the loop is running.
    2. achieve that different random values are chosen, also if two or more arrays have the same length


    Thank you for help!

    See code below:

    Code:
     string[] jn1 = { "a1", "b1", "c1", "d1", "e1", "f1" };
                string[] jn2 = { "a2", "b2", "c2" };
                string[] jn3 = { "a3", "b3", "c3", "c4" };
    
                int anzahljns1 = jn1.Length;
                int anzahljns2 = jn2.Length;
                int anzahljns3 = jn3.Length;
    
                do
                {
                    Random zufallszahl1 = new Random(0);
                    Random zufallszahl2 = new Random(0);
                    Random zufallszahl3 = new Random(0);
    
                    int jpo1 = zufallszahl1.Next(0, anzahljns1);
                    int jpo2 = zufallszahl2.Next(0, anzahljns2);
                    int jpo3 = zufallszahl3.Next(0, anzahljns3);
    
                    string first = jn1[jpo1];
                    string second = jn2[jpo2];
                    string third = jn3[jpo3];
    
                    Console.WriteLine(first + " "+ second + " " + third);
    
                    Console.WriteLine("...");
                    repeat = Console.ReadLine();
    
                } while (repeat == "y");

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: No "real" random numbers when using loop

    You've made (possibly) two mistakes. First, move all instantiations of the Random class (the lines that have new Random(0)) outside of the loop. It's enough to create the object once - and you don't need 3 of them, just 1.
    Then, don't pass the zero as the seed to the constructor - use the default one; use new Random(), unless you want your program to generate the same sequence of pseudorandom numbers every time.

    Then just call the Next method on the same random generator whenever you need a random number.

    When you use the default constructor, the seed is based on the time of the execution - creating the Random object inside a loop is a common error, because the loop usually executes really fast, so you get the same seed for many iterations.
    The same would happen if you created several Random objects one after another.
    So, just create one, and use that one for everything.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured