7

I mean the standard (?) random generator in .net /c#

Random random = new Random(seed);
random.next();

I know there are tens or hundreds of methods in literature, but I cannot find out which one the .net framework uses currently?

Reason for asking question: if I draw a LOT of random variables, will I ever return to the same sequence. I know some RNG have this undesirable property.

willem
  • 2,617
  • 5
  • 26
  • 38
  • You can [browse the framework source](http://msdn.microsoft.com/en-us/library/cc667410.aspx) yourself, or just [decompile it](http://wiki.sharpdevelop.net/ILSpy.ashx). – Kirk Woll Jun 24 '12 at 18:46
  • 1
    You can read the whole thing using justDecompile. Or, hell, isn't that part of the framework open source now? –  Jun 24 '12 at 18:46
  • I'd bet it uses http://en.wikipedia.org/wiki/Linear_congruential_generator, but I'm unable to find any proof – nothrow Jun 24 '12 at 18:47
  • 5
    (guys above - do you think that OP will be able to recognize the algorithm from it's source?) – nothrow Jun 24 '12 at 18:47
  • @Yossarian, it doesn't. That would be kind of the worst choice, anyway. – usr Jun 24 '12 at 18:48
  • @usr, why? There are other implementations in framework available, that would give better results. – nothrow Jun 24 '12 at 18:50
  • @willem all random number generators have a chance to come back to the same seed if you call them enough times. However, the RNG's list is so incredibly long that it is impractical to attempt to do so. – SomeGuy Feb 06 '14 at 17:43
  • possible duplicate of [How is a random number generated at runtime?](http://stackoverflow.com/questions/4440735/how-is-a-random-number-generated-at-runtime) – Ben Voigt May 05 '14 at 20:31

2 Answers2

11

Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.

http://msdn.microsoft.com/en-us/library/system.random.aspx

nothrow
  • 15,882
  • 9
  • 57
  • 104
1

All calculations are based on seed. If you define a seed then for the same sequence of methods you will get the same results. If not then the default seed is the Environment.TickCount, therefore the same sequence of methods will generate different results on each run (not guaranteed in parallels).