The same random numbers

LineCutter

Active Member
Licensed User
Longtime User
I might have missed this, but is there a way to specify a seed value to the rnd() function so that the same pseudo-random sequence is generated each time?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can do it with the Door library:
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Random.New1(False)
    Seed.New1(1)
    Seed.SetValue(0,75,"System.Int32") 'Set the seed value
    Random.CreateNew2("System.Random",Seed.Value)
    
    Msgbox(MyRnd(0,10))
    Msgbox(MyRnd(0,10))
End Sub

Sub MyRnd(Minimum, Maximum)
    Return Random.RunMethod3("Next",Minimum,"System.Int32",Maximum,"System.Int32")
End Sub
 

Attachments

  • Random.sbp
    542 bytes · Views: 176

LineCutter

Active Member
Licensed User
Longtime User
Thanks. That helps me create a password for the crypto question that agraham helped me with. Since it won't be physically present in the code it'll be that little bit harder for the casual hacker to bypass.
B4X:
AF0="A1B2C3D4E5F67890"
    n=""
    For i = 0 To 64
 n=n&StrAt(AF0,Random.RunMethod3("Next",0,"System.Int32",StrLength(AF0),"System.Int32"))
    Next
 

agraham

Expert
Licensed User
Longtime User
That helps me create a password for the crypto question that agraham helped me with.
That's a good idea! Why not take it a bit further and generate the characters directly? In this case printable ASCII ones. As an aside why 65 characters in n?

B4X:
n=""
For i = 0 To 64
   n=n&Chr(Random.RunMethod3("Next", 33, "System.Int32", 127, "System.Int32")))
Next
 

LineCutter

Active Member
Licensed User
Longtime User
The code was from an "obfuscate as hex values" routine from a test program, hence the limited characters used.

As for 65 values... well I'm clearly not a coder yet! :sign0148:
(Although it was an arbitrary number the intention was for 64 characters, regardless of the fact that they would then go through the MD5 -> DES.key processing you help me with a few days back)
 
Top