How reliable is the Random function in Delphi

2020-02-25 08:42发布

I am writing a program which write statistical tests in Delphi (must be Delphi) and I've heard that the Random functionality is somewhat odd. You have to call randomize to randomize the seed of the random function when the program starts.

I'm wondering if the random function (after calling randomize) is random enough for statistical tests or a Mersenne twister is needed? Does anyone have any insight into random's actual implementation which can tell me how important this is?

9条回答
迷人小祖宗
2楼-- · 2020-02-25 09:14

From the Embarcadero web site:

_lrand is the long random number generator function. _rand uses a multiplicative congruential random number generator with period 2^64 to return successive pseudo-random numbers in the range from 0 to 2^31 - 1.

The generator is reinitialized by calling srand with an argument value of 1. It can be set to a new starting point by calling srand with a given seed number.

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-25 09:15

If they didn't change the implementation since I analyzed it(Delphi 4 IIRC), the Delphi PRNG is implemented like this:

Randseed:=int32(Randseed*$08088405)+1
result:=Randseed*Range shr 32

(Pseudocode/assume the multiplications are on arbitrarily large integers)

查看更多
Melony?
4楼-- · 2020-02-25 09:16

Return random between 0..9

StrToInt(copy(FloatToStr(Random),4,1))

Note:Check FloatToStr(Random) length before use or use any other digit from the decimal part...

查看更多
登录 后发表回答