Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-03

Re: Re: random numbers

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vincent Snijders
to Antal: I also noticed this kind of behavior before, so that program was speccially made to track…
Antal wrote
As I noticed, when initialized the randseed with the same value, the whole rand values in the chain will be totally identical, which I'm afraid is rather far from what we might expect from a "real" random number generator!

Indeed, it isn't a real random number generator and in fact this is a good thing, otherwise you wouldn't be able to reproduce any behaviour with a Pseudo RNG. The fact it is Pseudo is advantageous in some cases.

Vincent

Antal
Show quoted text 27 lines
There is nothing strange about this. Randomize initialises randseed based on the system clock value expressed in seconds. So if you run it twice in the same second, you will get the same number. You are not generating a series of random numbers based on the Mersenne Twister here, because you only generate one random number per program run. If you take all these values "together" as supposedly belonging to a "series of random numbers", then the result is that you are using a different "random" generator, where the next "random" number is generated based on the next clock/time value as opposed to the previously generated random number. To test it properly, you should change your test program into this: var i: longint; begin Randomize; for i := 1 to 20 do writeln(random(394)); end. And then you have to compare the random numbers generated in one such a program run, not between different runs. Jonas

I also noticed this kind of behavior before, so that program was speccially made to track down the problem.
I know fully understand the behavior of the Randomize, so I might suggest you to initialise the randseed not based on seconds, but, preferrably on milliseconds, which renders a better approach of randomizing. I suppose it's not too complicated, but it can make hings better. As I noticed, when initialized the randseed with the same value, the whole rand values in the chain will be totally identical, which I'm afraid is rather far from what we might expect from a "real" random number generator!

Thanks,
Antal

Jonas Maebe
to Vincent Snijders: Indeed, it isn't a real random number generator and in fact this is a good thing,…
Vincent Snijders wrote 9 lines
> As I noticed, when initialized the randseed with the same value, > the whole rand values in the chain will be totally identical, > which I'm afraid is rather far from what we might expect from a > "real" random number generator! Indeed, it isn't a real random number generator and in fact this is a good thing, otherwise you wouldn't be able to reproduce any behaviour with a Pseudo RNG. The fact it is Pseudo is advantageous in some cases.

And this behaviour is indeed what many people expect, since it's exactly the same in TP and Delphi (and in C).

Jonas

Vinzent Hoefler
to Jonas Maebe
Jonas Maebe wrote 8 lines
On 9 mrt 2006, at 18:38, Vincent Snijders wrote: > > Indeed, it isn't a real random number generator and in fact this is > a good thing, otherwise you wouldn't be able to reproduce any > behaviour with a Pseudo RNG. The fact it is Pseudo is advantageous > in some cases. And this behaviour is indeed what many people expect,

Even _require_.