Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How reliable is the Random function in Delphi

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?

like image 882
Daisetsu Avatar asked Oct 15 '10 23:10

Daisetsu


2 Answers

Delphi's PRNG, like almost all programming language RTL PRNGs, is a linear congruential generator.

It's good enough for most small-scale things, but there are things to watch out for. In particular, watch out for low-order bits: the pattern of multiplication and add means that low-order bits are not very random at all. But this generally only applies to large 32-bit values pulled out and then truncated with mod or similar. Using Random(10) to pluck a value between 0 and 9 internally uses a multiplication over the whole 32-bit range rather than a mod operation.

like image 107
Barry Kelly Avatar answered Oct 04 '22 03:10

Barry Kelly


alt text

I couldn't resist.

like image 35
Mick Avatar answered Oct 04 '22 02:10

Mick