Reputation: 1036
when you create a .net
Random
object with no parameter, you get, as the docs put it:
Initializes a new instance of the Random class, using a time-dependent default seed value.
On the other side, you can specify the seed.
System.Random rand1 = new System.Random();
System.Random rand2 = new System.Random(222);
For rand2
the seed is known. How can I find out the seed of rand1
in order to obtain the same result at a different point in time?
Upvotes: 1
Views: 73
Reputation: 941873
The default constructor of the Random class uses Environment.TickCount as the seed. While you could technically use that property yourself to know the seed, it can't be 100.00% reliable since there is a very small chance that it changes between the two statements.
You'll have to do it like this instead so it is always safe:
int seed = Environment.TickCount;
System.Random rand1 = new System.Random(seed);
Upvotes: 6