How do random numbers from a random object work?

if i have this: (i already declared variables)

random1 = new Random();
Console.WriteLine(random1.Next(1, 100));
Console.WriteLine(random1.Next(1, 100));

      

When I have that, it will generate a different number each time console.writeline is called, so this will generate ex. 10, 55, and if you do it again 20.60, then mostly random numbers every time, okay. But when I add this:

random2 = new Random();
Console.WriteLine(random1.Next(1, 100));
Console.WriteLine(random1.Next(1, 100));
Console.WriteLine(random2.Next(1, 100));
Console.WriteLine(random2.Next(1, 100));

      

Random1 will generate the same numbers as random! It will be so. 5,54,5,54 and if I do it again 70,34,70,34. But it random2 is a new object, so why is it generating the same numbers as random1 ??

Another example: If I have a class like this

 class RandomNumber
    {
        Random random = new Random();
        public int getrandomnumber { get { return random.Next(1, 5); } }
    }

      

Thereafter

randomnumberobject = new RandomNumber();
randomnumberobject2 = new RandomNumber();
Console.WriteLine(randomnumberobject.getrandomnumber);
Console.WriteLine(randomnumberobject2.getrandomnumber);

      

They will generate a random number, but both of them will generate the same random number. So the first time I get this is 5.5 seconds, it's 3.3, and so on. But if I change the class to this

class RandomNumber
    {
        Random random;
        public int getrandomnumber { get { return random.Next(1, 5); } }
        public RandomNumber(Random random) { this.random = random; }
    }

      

And do this instead

   random = new Random();
   randomnumberobject = new RandomNumber(random);
   randomnumberobject2 = new RandomNumber(random);
   Console.WriteLine(randomnumberobject.getrandomnumber);
   Console.WriteLine(randomnumberobject2.getrandomnumber);

      

All of a sudden, they generate different random numbers! So why is this happening? What reason? Keep in mind, I am still a beginner.

+3


source to share


3 answers


What you see is "by design" - that is, due to how the class is implemented ... The algorithm it uses requires a "seed" (like an initial state), which in this case is clock-based .. . as described on MSDN :

The random number generation starts from the seed. If the same seed is reused, the same series of numbers is generated. One way to get different sequences is to make the initial value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its initial value, while its parameterized constructor can take an Int32 value based on the number of ticks at the current time. However, since the clock is finite resolution, using a parameterless constructor to create different random objects in immediate sequence creates a random number that generates identical sequences of random numbers. the following example illustrates,that two random objects that are created in close sequence generate an identical sequence of random numbers.



This means that if two objects Random

are created almost simultaneously using the parameterless constructor, they will generate the same sequence of random numbers, since they were initialized with the same clock-based "initial state".

+5


source


The generation of random numbers is based on seed in most programming languages. This seed is usually taken from the current time value. Therefore, if you declare two random objects close to each other, most likely they will receive the same seed from the CLR. This is why they will generate the same numbers.

When you use the same instance, the first two numbers will already come from a random sequence, so the next two will not be the same.



Use explicit seed in situations like this (Random constructor, which is overloaded with int).

0


source


Since you transplant each time in the last example, if you don't overload a random object, it will pull from one pool of random numbers each time.

        var random1 = new Random(DateTime.Now.Millisecond);
        Console.WriteLine(random1.Next(1, 100));
        Console.WriteLine(random1.Next(1, 100));
        Console.WriteLine(random1.Next(1, 100));
        Console.WriteLine(random1.Next(1, 100));

        var random2 = new Random(DateTime.Now.Millisecond);
        Console.WriteLine(random2.Next(1, 100));
        Console.WriteLine(random2.Next(1, 100));

      

0


source







All Articles