Does every machine generate same result of random number by using the same seed?

后端 未结 4 1895
独厮守ぢ
独厮守ぢ 2021-01-02 02:15

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

Random rand = new Random(3412);

The ran

4条回答
  •  灰色年华
    2021-01-02 02:53

    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?

提交回复
热议问题