April 25, 2019, 04:18:19 AM

Author Topic: [Bmx] Can I rely on Rand function to generate identical sequences?  (Read 241 times)

Offline wookie22

  • Jr. Member
  • **
  • Posts: 22
    • Hidden Deep
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?
Hidden Deep - 2D Action & Exploration Sci-Fi Thriller for PC/Mac/Linux
http://www.hiddendeepgame.com

Offline Yellownakji

  • Full Member
  • ***
  • Posts: 171
  • Whoop-de-doo-ba-dooo!
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #1 on: March 24, 2019, 04:41:36 PM »
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.

Offline GW

  • Full Member
  • ***
  • Posts: 156
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #2 on: March 24, 2019, 06:02:11 PM »
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

Offline Matty

  • Hero Member
  • *****
  • Posts: 629
    • MattiesGames
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #3 on: March 24, 2019, 09:56:00 PM »
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)

Offline col

  • Sr. Member
  • ****
  • Posts: 415
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #4 on: March 25, 2019, 02:07:21 AM »
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
Code: [Select]
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
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #5 on: March 25, 2019, 06:23:48 AM »
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

Offline wookie22

  • Jr. Member
  • **
  • Posts: 22
    • Hidden Deep
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #6 on: March 25, 2019, 12:56:58 PM »
@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).
Hidden Deep - 2D Action & Exploration Sci-Fi Thriller for PC/Mac/Linux
http://www.hiddendeepgame.com

Offline col

  • Sr. Member
  • ****
  • Posts: 415
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #7 on: March 25, 2019, 02:29:37 PM »
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.
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #8 on: March 25, 2019, 02:29:52 PM »
Mine was run on a linux box containing an AMD processor.


Bye
Ron

Offline wookie22

  • Jr. Member
  • **
  • Posts: 22
    • Hidden Deep
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #9 on: March 25, 2019, 02:46:02 PM »
Thanks for testing. So try that ultimate test on AMD:

Code: [Select]
SeedRnd(178818223)

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

My result (Intel I3): 3.2892029219375827
Hidden Deep - 2D Action & Exploration Sci-Fi Thriller for PC/Mac/Linux
http://www.hiddendeepgame.com

Offline col

  • Sr. Member
  • ****
  • Posts: 415
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #10 on: March 25, 2019, 03:02:29 PM »
On all CPUs and OSs I get
3.2892029219375827
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline wookie22

  • Jr. Member
  • **
  • Posts: 22
    • Hidden Deep
Re: [Bmx] Can I rely on Rand function to generate identical sequences?
« Reply #11 on: March 25, 2019, 06:23:53 PM »
Thanks :)
Hidden Deep - 2D Action & Exploration Sci-Fi Thriller for PC/Mac/Linux
http://www.hiddendeepgame.com