How reliable is the Random function in Delphi
Asked Answered
H

9

12

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?

Hondo answered 15/10, 2010 at 23:29 Comment(3)
Having to seed a randomizer function is not at all uncommon - you have to do the same thing with a Mersenne twister.Florance
And the possibility to seed a random function it's often a great benefit. By using the same seed you can duplicate your results, which can be of great help, for instance while debugging.Wynd
There is an excellent discussion of this topic in chapter 6 of Julian Bucknall's book "Tomes of Delphi: Algorithms And Data Structures" (www.boyet.com)Wicklow
I
6

Whether Random is sufficiently reliable for your statistical tests will depend on the context in which you intend to use it.

Having said that, I have written several pieces of Delphi code that need to do proper statistics, and have used Random e.g. for obtaining various null distributions, data pseudo-replications and resamplings. So far, I have not come across any case in my own code where Random would have yielded biased or unreliable results, or results which would have precluded its use for the intended statistical test. But what holds for my code does not necessarily have to hold for yours.

If in doubt, you could of course statistically analyse the results of calls to Random (e.g. in R, SPSS, etc.) and examine whether the distribution of results violate the distributional requirements for your particular statistical test(s). [If you're a proper scientist, this is what you should do anyway.]

Should you need other PRNGs - e.g. the TPMath library contains some. (For more involved things, there's also the option of calling elaborate statistical functions from R via Delphi.)

Issy answered 16/10, 2010 at 12:34 Comment(0)
H
20

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.

Halfhour answered 16/10, 2010 at 0:47 Comment(0)
I
17

alt text

I couldn't resist.

Immanent answered 16/10, 2010 at 0:46 Comment(4)
I like humour. But it must be CW!Catalysis
Nah, no CW, it perfectly illustrates that what we may perceive as non-random, actually can be perfectly random. After all, random isn't the absense of a pattern...Subclavian
Any time you feel inclined to put "I couldn't resist" in your "answer", you should be inclined to make it CW. It's a matter of degree, but in this case there was no direct answer to the OP.Boscage
I'm fine with people down-voting this. I don't have the power to make this a community wiki. Vote up/down, I really don't mind...I'm not trying to game SO's reputation system.Immanent
S
6

If you are seeking a way to guarantee uniqueness of random numbers with the fastest execution time, About.com has created a challenge on Fastest Unique Random Number Generator, and Patrick van Logchem's implementation has been elected as the winner.

Spectroradiometer answered 16/10, 2010 at 1:56 Comment(0)
I
6

Whether Random is sufficiently reliable for your statistical tests will depend on the context in which you intend to use it.

Having said that, I have written several pieces of Delphi code that need to do proper statistics, and have used Random e.g. for obtaining various null distributions, data pseudo-replications and resamplings. So far, I have not come across any case in my own code where Random would have yielded biased or unreliable results, or results which would have precluded its use for the intended statistical test. But what holds for my code does not necessarily have to hold for yours.

If in doubt, you could of course statistically analyse the results of calls to Random (e.g. in R, SPSS, etc.) and examine whether the distribution of results violate the distributional requirements for your particular statistical test(s). [If you're a proper scientist, this is what you should do anyway.]

Should you need other PRNGs - e.g. the TPMath library contains some. (For more involved things, there's also the option of calling elaborate statistical functions from R via Delphi.)

Issy answered 16/10, 2010 at 12:34 Comment(0)
K
4

Unless you buy some relatively esoteric hardware, the best approximation to random numbers a computer can provide is a completely deterministic pseudorandom sequence. In general, the randomize function uses some relatively random value (often based on the time, but sometimes on mouse movements - I have no idea what Delphi does) as a seed which provides the entry point to the pseudorandom sequence. Without this, you will end up getting back the same set of random numbers in the same order each time, which tends to defeat the purpose of using random numbers in the first place.

Okay, I realize that this doesn't answer the question about reliability, but it should give you some confidence that requiring you to call randomize is a sign of a good generator rather than of a bad one. There are a bunch of statistical tests which show how random a sequence of numbers is, and it is likely that the Delphi random number generator is suitable for many purposes as it is a mature product.

Klapp answered 16/10, 2010 at 0:0 Comment(0)
H
3

Just to add to the pool of possibilities - Windows offers a range of built-in Cryptography functions. There probably is a Delphi wrapper for them as well, if it's not already included by default.

Among these functions is also a cryptographically strong random number generator. This is by far the best randomness you will get in software, because it seeds itself based on a very long list of factors. I'm not sure, but I suspect it will even use a hardware random number generator if you have one.

And if that's not enough, you can also try to sign up at the Quantum Random Bit Generator Service for some REALLY random values.

Houppelande answered 17/10, 2010 at 15:49 Comment(0)
M
2

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.

Mendel answered 17/10, 2010 at 15:35 Comment(0)
M
2

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)

Marhtamari answered 17/10, 2010 at 15:46 Comment(0)
B
-2

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...

Berns answered 11/9, 2012 at 10:4 Comment(2)
The recommended call to generate a random integer in the range 0 through 9 is "random(10)" Using floattostr is clever, but the expression given will sometimes fail. Consider: " randseed := -1498392781; X := StrToInt(copy(FloatToStr(Random),4,1));" In this case random will return exactly 0.5, floattostr will return "0.5", since there is no fourth character, copy will return empty string, and StrToInt will fail with an exception.Araucanian
The idea was to give example with bigger dispersion (see link).So You can freely enhance this algorithm according Your needs...Berns

© 2022 - 2024 — McMap. All rights reserved.