RandomSeed across versions and platforms

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 2083
Joined: Tue Dec 23, 2003 3:54 am

RandomSeed across versions and platforms

Post by kenmo »

Question:

I want to generate a sequence of random integers using Random() or RandomData(), but it needs to be repeatable any time the program is run... Obviously the solution is to use RandomSeed() with some desired seed to guarantee the same sequence each time!

But to save myself from any future bugs, I'm wondering if there is any possibility for RandomSeed() to give different results in a future version of PureBasic, OR in any of the current versions (4.60 in Mac, Linux, Windows 64-bit -- I currently use Win 32).

Perhaps I am safe, and a good algorithm was chosen 10 years ago, and Random/RandomSeed hasn't changed since?

But I know that some libraries are different across platforms (eg. the Packer algorithm?) and I want to be sure that a Mac or Linux port won't result in different random sequences.
User avatar
idle
Always Here
Always Here
Posts: 6235
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: RandomSeed across versions and platforms

Post by idle »

If I remember PB uses the Mersenne twister prng, it's not likely to change
and it should produce the same results on all platforms from a given seed

Code: Select all

RandomSeed(1234)
Debug Random(1000)
;271 ubuntu 11.10 x86
Windows 11, Manjaro, Raspberry Pi OS
Image
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: RandomSeed across versions and platforms

Post by wilbert »

I think the results are the same between different platforms
You can always use your own generator if you want to be absolutely sure.
I posted two a little while ago ( http://www.purebasic.fr/english/viewtop ... 12&t=49263 )
There are also simple ones with only a few lines of code. It depends what you need them for what works for you.
User avatar
kenmo
Addict
Addict
Posts: 2083
Joined: Tue Dec 23, 2003 3:54 am

Re: RandomSeed across versions and platforms

Post by kenmo »

Thanks guys. It seems consistent across Linux and Windows x86 at least.

I have implemented what I needed using Random(), and when I port it to Mac / Win x64 later on I will double check it though.

If the PB team used a standard generator and implemented it themselves, I suppose there's no probable reason for it to be different across OS'es.
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: RandomSeed across versions and platforms

Post by freak »

It produces the same output on all OS and processor types. The algorithm also didn't change in any PB version so far and I see no reason why it should in any future one.

The 64bit versions can use a 'Maximum' parameter in the quad range and the 32bit versions are limited to longs, but if you use a value in the long range the output will be identical on 32bit and 64bit.
quidquid Latine dictum sit altum videtur
Post Reply