Create multiple random class with different seeds simultaneously

suggest change

Two Random class created at the same time will have the same seed value.

Using System.Guid.NewGuid().GetHashCode() can get a different seed even in the same time.

Random rnd1 = new Random();
Random rnd2 = new Random();
Console.WriteLine("First 5 random number in rnd1");
for (int i = 0; i < 5; i++)
    Console.WriteLine(rnd1.Next());

Console.WriteLine("First 5 random number in rnd2");
for (int i = 0; i < 5; i++)
    Console.WriteLine(rnd2.Next());

rnd1 = new Random(Guid.NewGuid().GetHashCode());
rnd2 = new Random(Guid.NewGuid().GetHashCode());
Console.WriteLine("First 5 random number in rnd1 using Guid");
for (int i = 0; i < 5; i++)
    Console.WriteLine(rnd1.Next());
Console.WriteLine("First 5 random number in rnd2 using Guid");
for (int i = 0; i < 5; i++)
    Console.WriteLine(rnd2.Next());

Another way to achieve different seeds is to use another Random instance to retrieve the seed values.

Random rndSeeds = new Random();
Random rnd1 = new Random(rndSeeds.Next());
Random rnd2 = new Random(rndSeeds.Next());

This also makes it possible to control the result of all the Random instances by setting only the seed value for the rndSeeds. All the other instances will be deterministically derived from that single seed value.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Generating Random Numbers:
* Create multiple random class with different seeds simultaneously

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
144 Generating Random Numbers
147 C# Script