Does any machine generate the same random number result using the same seed?

I'm stuck in a random generator right now. The requirements specification shows an example similar to this:

Random rand = new Random(3412); 

The rand result is not directly reported, but used for other characteristics.

I wrote the same code as above to generate a random number with a seed of 3412. however, the result of the rest of the work is completely different from the sample.

The result of generating 518435373, I used the same code that I tried to use in the C # online compiler, but getting a different generation result, which is 11688046, the result of the rest of the results was also different from the sample.

So I'm just wondering what should be different on different machines?

By the way, can anyone provide the result from your machine, just look if this looks like me.

+8
c # random seed
source share
4 answers

I would expect that any one implementation will give the same sequence for the same seed, but there may be different implementations. For example, the "C # online compiler" might end up using Mono, and I expect it to have a different implementation in .NET.

I don’t know if the implementations have changed between .NET versions, but again this seems quite possible.

The documentation for the Random(int) constructor reads:

Providing the same seed value to different Random objects causes each instance to create identical sequences of random numbers.

... but it does not indicate the consequences of different versions, etc. Heck, it does not even indicate whether the x86 and x64 versions will give the same results. I would expect the same results in any particular CLR instance (i.e. One process, not two CLRs working side by side, or *.

If you need something more stable, I would start with a certain algorithm - I am sure that versions of Mersenne Twister are available, etc.

+14
source share

This is not indicated as accepting such a promise, so you must assume that it is not.

A good rule with any specification is not to make promises that are not required for reasonable use, so you are freer to improve the situation later.

In fact, the Random documentation says:

The current implementation of the Random class is based on the algorithm of the random number generator Donald E. Knuth.

Pay attention to the phrase "current implementation", implying that it may change in the future. This is very convincing evidence that not only does not promise to be compatible between versions, but there is no intention.

If the specification requires consistent pseudo-random numbers, then it should indicate the algorithm as well as the initial value. In fact, even if Random was indicated as such a promise, what if you need a non-.NET implementation of all or part of your specification - or something that interacts with it - in the future?

+6
source share

Perhaps this is due to the different versions of the framework. Take a look

+1
source share

The ISP you tried can use the Mono CLR implementation, which is different from the one provided by Microsoft. Therefore, probably their implementation of the Random class is slightly different.

0
source share

All Articles