Does every machine generate same result of random

2020-02-28 04:32发布

I'm current stuck in the random generator. The requirement specification shows a sample like this:

Random rand = new Random(3412);

The rand result is not directly given out, but used for other performance.

I'd written the same code as above to generate a random number by a seed 3412. however, the result of the rest performance is totally different with sample.

The generating result is 518435373, I used the same code tried on the online c# compiler, but getting different result of generation which is 11688046, the rest performance result was also different with the sample.

So I'm just wondering is that supposed to be different in different machines?

BTW, could anyone provide the result from your machine just see if it's same with me.

4条回答
ゆ 、 Hurt°
2楼-- · 2020-02-28 04:56

The online provider you tried might use the Mono implementation of the CLR, which is different of the one Microsoft provides. So probably their Random class implementation is a bit different.

查看更多
混吃等死
3楼-- · 2020-02-28 05:02

It isn't specified as making such a promise, so you should assume that it does not.

A good rule with any specification, is not to make promises that aren't required for reasonable use, so you are freer to improve things later on.

Indeed, Random's documentation says:

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

Note the phrase "current implementation", implying it may change in the future. This very strongly suggests that not only is there no promise to be consistent between versions, but there is no intention to either.

If a spec requires consistent pseudo-random numbers, then it must specify the algorithm as well as the seed value. Indeed, even if Random was specified as making such a promise, what if you need a non-.NET implementation of all or part of your specification - or something that interoperates with it - in the future?

查看更多
干净又极端
4楼-- · 2020-02-28 05:05

I would expect any one implementation to give the same sequence for the same seed, but there may well be different implementations involved. For example, an "online C# compiler" may well end up using Mono, which I'd expect to have a different implementation to the one in .NET.

I don't know whether the implementations have changed between versions of .NET, but again, that seems entirely possible.

The documentation for the Random(int) constructor states:

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.

... but it doesn't specify the implications of different versions etc. Heck, it doesn't even state whether the x86 and x64 versions will give the same results. I'd expect the same results within any one specific CLR instance (i.e. one process, and not two CLRs running side-by-side, either*.

If you need anything more stable, I'd start off with a specified algorithm - I bet there are implementations of the Mersenne Twister etc available.

查看更多
▲ chillily
5楼-- · 2020-02-28 05:20

This is probably due to different framework versions. Have a look at this

查看更多
登录 后发表回答