SyntaxBomb - Indie Coders

Languages & Coding => BlitzMax / BlitzMax NG => Topic started by: wookie22 on March 24, 2019, 14:51:52

Title: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: wookie22 on March 24, 2019, 14:51:52
As Rand is based on floating point operations I'm wondering if giving the same seed I will get the same results on every machine. Anyone know something about it?
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: Yellownakji on March 24, 2019, 16:41:36
Quote from: wookie22 on March 24, 2019, 14:51:52
As Rand is based on floating point operations I'm wondering if giving the same seed I will get the same results on every machine. Anyone know something about it?


If i'm not mistaken, RAND uses the system-clock to generate a number.   I don't think otherwise.

You could create your own seed system.   It's not super super tricky, just time consuming.    Every letter you enter will update one or more variables.  That's how certain games like minecraft handle seeds.
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: GW on March 24, 2019, 18:02:11
I can't say for sure from my experience, however Bmax has been win/mac/linux from the beginning (2004) and I can't recall issue coming up.

[edit]
I did find a post from 2011[1] where someone claims it's not deterministic and suggests Bruceys random module.  I've replaced the brl random with wtih bruceys random module years ago as it produced better random numbers in general.

[1]http://mojolabs.nz/posts.php?topic=94419
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: Matty on March 24, 2019, 21:56:00
Here's a random number generator

Look in the functions here.....'seed' is a global variable.

http://www.mattiesgames.com/learntocode/engine/functions.js

Your own deterministic random number generator.....basically same as what VBA in Excel uses (methodology)
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: col on March 25, 2019, 02:07:21
Rather than everyone guessing the answer why don't we post our results to find out.


Here's some results from test units I have here using the standard BlitzMax 1.50

SeedRnd 1118272

For k=1 To 10
Print Rnd(10.10)
Next


Windows 10 Pro
5.5124371326399269
1.2384022145700284
0.35361654399026321
1.8116738415466080
4.1793979756309891
0.54494589303085272
6.6475921139506013
8.2621353119825383
5.9340719629725021
10.026912050562693

Windows 7 Home
5.5124371326399269
1.2384022145700284
0.35361654399026321
1.8116738415466080
4.1793979756309891
0.54494589303085272
6.6475921139506013
8.2621353119825383
5.9340719629725021
10.026912050562693

Linux 32bit Ubuntu VM
5.5124371326399269
1.2384022145700284
0.35361654399026321
1.8116738415466080
4.1793979756309891
0.54494589303085272
6.6475921139506013
8.2621353119825383
5.9340719629725021
10.026912050562693
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: Derron on March 25, 2019, 06:23:48
Get the very same numbers - yet I am not 100% sure if that happens on every computer because of said FP determinism problem.  As we use "double" here it might already help in most cases when we deal with floats instead (potential loss of accuracy).

Maybe of interest:
https://github.com/bmx-ng/brl.mod/issues/84
-> size of the numbers matters in some cases.


bye
Ron
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: wookie22 on March 25, 2019, 12:56:58
@Col Are all these machines on Intel CPU? I think that processor is important here. Also I think you should made 1mil iterations (just print every 100 000th and the last).
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: col on March 25, 2019, 14:29:37
Quote@Col Are all these machines on Intel CPU?

Hiya,

I used a mixture of CPUs:
Intel i5-4440 - Ubuntu
Intel i7-3610 - Win7, Win10

And I've also just tried with a Ryzen5 2600X on Win10 to use a different chip manufacturer and got the same results.

Are you getting similar results?

Hope it helps.
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: Derron on March 25, 2019, 14:29:52
Mine was run on a linux box containing an AMD processor.


Bye
Ron
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: wookie22 on March 25, 2019, 14:46:02
Thanks for testing. So try that ultimate test on AMD:


SeedRnd(178818223)

Local r:Double
For Local j% = 1 To 1000000
r = Rnd(0, 10)
Next
print r


My result (Intel I3): 3.2892029219375827
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: col on March 25, 2019, 15:02:29
On all CPUs and OSs I get
3.2892029219375827
Title: Re: [Bmx] Can I rely on Rand function to generate identical sequences?
Post by: wookie22 on March 25, 2019, 18:23:53
Thanks :)