Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is class Random seeded with when no seed is provided?

Tags:

c#

.net

random

Class Random can be instantiated using a constructor without parameters and MSDN says that in this case it is seeded with some time-dependent value.

Is the way to derive that time-dependent value documented anywhere? Can I reproduce it?

like image 807
sharptooth Avatar asked Feb 20 '23 16:02

sharptooth


1 Answers

It's not documented, and I think that's on purpose. I don't see any good reason why something like this should be documented and the framework implementers should be able to choose how exactly to do this.

But if you want to know how does it work currently, just use ildasm or Reflector. Reflector will give you this (.Net 4.5 beta):

public Random() : this(Environment.TickCount)
{
}

If you look at the source code of mono, you will see that since 2003, it does exactly the same thing.

like image 122
svick Avatar answered Feb 23 '23 20:02

svick