B4J Question Do All Platforms Use Same RNG?

cklester

Well-Known Member
Licensed User
Does using the same seed across platforms generate the same random number sequence?
 

cklester

Well-Known Member
Licensed User
Yes. I have a game app that works cross-platform but needs to have the exact same RNG on each platform so that the players experience the exact same environment and situation. I want everyone to be able to compete (track high scores) with everyone else, regardless of platform, and that requires they all work with the same sequence of random numbers.

So, if I wanted the same random number sequence across platforms, I would have to use a server solution. Not sure how robust that will be, but it seems the only way to insure everyone plays the exact same levels.

Any other ideas?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Simple solution:
1. Create a "random file" with 250k integers:
B4X:
Dim b(1000000) As Byte
Dim sr As SecureRandom 'Encryption library
sr.GetRandomBytes(b)
File.WriteBytes(File.DirApp, "random.dat", b)

2. Create a class named NotRandom:
B4X:
Sub Class_Globals
    Private mIndex As Int
    Private mLength As Int
    Private mSkipLength As Int
    Private raf As RandomAccessFile
End Sub

Public Sub Initialize (Start As Int, SkipLength As Int)
    Dim b() As Byte = File.ReadBytes(File.DirAssets, "random.dat")
    raf.Initialize3(b, True)
    mLength = raf.Size / 4
    mIndex = Start Mod mLength
    mSkipLength = SkipLength
End Sub

Public Sub NextInt As Int
    mIndex = (mIndex + mSkipLength) Mod mLength
    Return raf.ReadInt(mIndex * 4)
End Sub

Public Sub NextInt2 (MinValue As Int, MaxValue As Int) As Int
    Dim delta As Int = MaxValue - MinValue
    Dim i As Int = NextInt
    If i < 0 Then i = -i
    Return (i Mod delta) + MinValue
End Sub

Usage example:
B4X:
Dim nr As NotRandom
nr.Initialize(10, 230)
For i = 1 To 100
    Log(nr.NextInt2(0, 10))
Next
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Also correct solution, perhaps somewhat limited. :)

1605517863202.png


https://xkcd.com/221/
 
Upvote 0

cklester

Well-Known Member
Licensed User
Ha! One of my ideas was exactly that: create a huge file of randomly generated integers. But I didn't know what to do if one time I need 0 to 10 and another time I needed 100 to 500. Thanks for the lesson! ?

B4X:
Simple solution:
1. Create a "random file" with 250k integers:
'...
Public Sub NextInt2 (MinValue As Int, MaxValue As Int) As Int
    Dim delta As Int = MaxValue - MinValue
    Dim i As Int = NextInt
    If i < 0 Then i = -i
    Return (i Mod delta) + MinValue
End Sub

Usage example:
B4X:
Dim nr As NotRandom
nr.Initialize(10, 230)
For i = 1 To 100
    Log(nr.NextInt2(0, 10))
Next
 
Upvote 0

cklester

Well-Known Member
Licensed User
This is slightly off-topic, but it came up: Can the Initialize Sub in a Class be called something else, like New?

2. Create a class named NotRandom:
B4X:
Public Sub New (Start As Int, SkipLength As Int) 'instead of Initialize
    Dim b() As Byte = File.ReadBytes(File.DirAssets, "random.dat")
    raf.Initialize3(b, True)
    mLength = raf.Size / 4
    mIndex = Start Mod mLength
    mSkipLength = SkipLength
End Sub
 
Upvote 0
Top