3

I'm using System.Random and I was wondering. If I provide a specific seed, will the sequence of random numbers be the same on every computer on which the code is run? Will it continue to be the same with future releases (i.e. is it built into the spec?)

Jordan
  • 9,642
  • 10
  • 71
  • 141
  • well, then that wouldn't be "random" would it? – D Stanley Jul 02 '14 at 18:16
  • 1
    Software random number generators are not random. They depend upon a seed. The only way to make them appear random is to provide a current time value. It still isn't random, but the seed is unknown. And why am I getting voted down? – Jordan Jul 02 '14 at 18:22
  • My point is - if you want a "random" sequence, why do you care if works the same on different computers and different .NET releases? – D Stanley Jul 02 '14 at 18:25
  • @DStanley - read the question. It is valid. – H H Jul 02 '14 at 18:30
  • I need a random sequence of bytes for a given seed and that random sequence needs to be dependable. If the answer here were negative, i would then write my own seed based random number generator. I need deterministic randomness. That's not uncommon. That's how the old windows Solitaire could produce the same "random" sequence of cards from a user provided seed. I'm not writing a game and this dependability is more critical in my case. – Jordan Jul 02 '14 at 18:33
  • @HenkHolterman It's a valid question; I'm just curious about the use case for a "random" sequence that must be the same on every computer and .NET version. (I didn't vote it down, BTW) – D Stanley Jul 02 '14 at 18:34
  • To be clear, I didn't think that you did vote me down. – Jordan Jul 02 '14 at 18:35
  • @Jordan OK That scenario makes sense. Thanks. – D Stanley Jul 02 '14 at 18:38

1 Answers1

5

If I provide a specific seed, will the sequence of random numbers be the same on every computer on which the code is run?

Yes. That is the point of a seed.

Will it continue to be the same with future releases (i.e. is it built into the spec?)

Most likely, but this isn't guaranteed. The documentation for System.Random states:

The current implementation of the Random class is based on 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.

The wording there does leave it open for a future implementation to change algorithms.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • The documentation also states that you should not rely on the same seed producing the same pseudo-random sequence across framework versions. It is known to have changed in the [past](http://stackoverflow.com/questions/9758472/implementation-change-to-nets-random). – Mike Zboray Jul 02 '14 at 19:24
  • @mikez Yeah - within the same framework version/platform it should be safe (across computers, etc), but not necessarily across frameworks or platforms. – Reed Copsey Jul 02 '14 at 19:25