Sign in Join

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

Mailing lists / fpc-pascal / 2006-03

Re: Random numbers

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Antal
Show quoted text 10 lines
|Random numbers should not be generated with a method chosen at random. | -- Donald E. Knuth |The generation of random numbers is too important to be left to chance. | -- Robert R. Coveyou, Oak Ridge National Laboratory, 1969 The Mersenne Twister Free Pascal uses is one of the best PRNGs known today, it just has to be used the right way. But calling it from several threads and "randomly" overwriting its state array is definitely not the right way to use it.
I'm disappointed, because I compiled the following program (on linux):
rnd.pp
begin
Randomize;
writeln(random(394));
end.

and I ran it for 11 times, and the results are rather strange: 310.
198
344
322*
322*
317-
317-
192
336.
382.
339.
(upon noting the dups I had to re-run again twice, getting:) 120
297.

so, I've got two times a repetition, though I only re-ran the program. (noted with * and - )
Then I made a new series of random generating:

297.
120
310.
382.
336.
192
317
322
344
198
339.

I only have two problems with these random numbers: Firstly, I can notice a repetition of some "random" numbers (noted with ".")
Then also 70% of them are 3xx
I learnt about random numbers, that they do not behave by this way.

Anyway, random numbers are behaving randomly, and we also had a big scandal about the national lottery, when (using mechanical methods), the occurence of "47" was too frequent in the last ten years, and also last year we had around 50% without winners :) (which added hundreds of thousands of EURO to the company manager's salary)

So I am really happy with FPC, but this was a bit strange for me. So it wasn't running simultaneously, but just executing the binary file by hand one after another.

I also agree with you Vincent, random numbers must be taken extremly carefully. But they also have to care for us :)
Actually I can't recall (though I studied it 10 years ago) which functions should increase the entropy.

Jonas Maebe
to Antal
Antal wrote 26 lines
> The Mersenne Twister Free Pascal uses is one of the best PRNGs known > today, it just has to be used the right way. But calling it from > several threads and "randomly" overwriting its state array is > definitely not the right way to use it. I'm disappointed, because I compiled the following program (on linux): rnd.pp begin Randomize; writeln(random(394)); end. and I ran it for 11 times, and the results are rather strange: 310. 198 344 322* 322* 317- 317- 192 336. 382. 339. (upon noting the dups I had to re-run again twice, getting:) 120 297.

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.

Show quoted text 21 lines
so, I've got two times a repetition, though I only re-ran the program. (noted with * and - ) Then I made a new series of random generating: 297. 120 310. 382. 336. 192 317 322 344 198 339. I only have two problems with these random numbers: Firstly, I can notice a repetition of some "random" numbers (noted with ".") Then also 70% of them are 3xx I learnt about random numbers, that they do not behave by this way.

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

Jonas Maebe
to Jonas Maebe
Jonas Maebe wrote
And then you have to compare the random numbers generated in one such a program run, not between different runs.

And note that even then you may get two times the same number after each other. There is nothing wrong with that though, since if that couldn't happen, that would make the next number more and not less predictable (since then you would know in advance the next number will not be the same as the previous one).

Jonas

Vinzent Hoefler
to Antal: I'm disappointed, because I compiled the following program (on linux): rnd.pp begin…
Antal wrote 11 lines
> The Mersenne Twister Free Pascal uses is one of the best PRNGs > known today, it just has to be used the right way. But calling it > from several threads and "randomly" overwriting its state array is > definitely not the right way to use it. I'm disappointed, because I compiled the following program (on linux): rnd.pp begin Randomize; writeln(random(394)); end.

Again: You are using a PRNG the wrong way, Jonas already pointed that out.

Randomize is supposed to be called exactly once and then never again and after then a (rather large) series of random number should be generated. The way you use it, by reinitializing a PRNG before each generation of a single random number, basically means all you get is the "entropy" of the randomizer (the system clock) and not the one of the PRNG. Well, last time I checked, the system clock is not exactly random.

Vinzent.

Alain Michaud
to Jonas Maebe: There is nothing strange about this. Randomize initialises randseed based on the system…

Hi,

I am not a mathematician myself, but I predict that unless you make extensive tests over a very long period of time you will never find any corelations!

Jonas Maebe wrote 81 lines
On 6 mrt 2006, at 13:37, Antal wrote: >> The Mersenne Twister Free Pascal uses is one of the best PRNGs known >> today, it just has to be used the right way. But calling it from >> several threads and "randomly" overwriting its state array is >> definitely not the right way to use it. > I'm disappointed, because I compiled the following program (on linux): > rnd.pp > begin > Randomize; > writeln(random(394)); > end. > > and I ran it for 11 times, and the results are rather strange: > 310. > 198 > 344 > 322* > 322* > 317- > 317- > 192 > 336. > 382. > 339. > (upon noting the dups I had to re-run again twice, getting:) > 120 > 297. 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. > so, I've got two times a repetition, though I only re-ran the program. > (noted with * and - ) > Then I made a new series of random generating: > > 297. > 120 > 310. > 382. > 336. > 192 > 317 > 322 > 344 > 198 > 339. > > I only have two problems with these random numbers: > Firstly, I can notice a repetition of some "random" numbers (noted > with ".") > Then also 70% of them are 3xx > I learnt about random numbers, that they do not behave by this way. 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 _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal