iOS Question Generate a random number between two numbers

Mark Turney

Active Member
Licensed User
What about for a TRNG? I would like clarification please on using the iEncryption library for instance to generate a TRUE random number between "this" and "that" using perhaps some measure from attitude or acceleration as a seed.

Thanks!
 

Erel

Administrator
Staff member
Licensed User
Secure random generator with the code that tests it:
B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.RootPanel.LoadLayout("1")
   Nav.ShowPage(Page1)
   Dim m As Map
   m.Initialize
   For i = 1 To 500
     Dim res As Int = SecureRandomGenerator(-10, 10)
     m.Put(res, m.GetDefault(res, 0) + 1)
   Next
   Log(m)
End Sub

Sub SecureRandomGenerator(Minimum As Int, Maximum As Int) As Int
   Dim sr As SecureRandom
   Dim data(4) As Byte
   sr.GetRandomBytes(data)
   Dim bc As ByteConverter
   Dim random As Int = bc.IntsFromBytes(data)(0)
   Maximum = Maximum - 1
   Dim d As Double = random / 0x7FFFFFFF
   Dim res As Int = Round(d * (Maximum - Minimum + 1) / 2 + (Minimum + Maximum) / 2)
   'Log(res & ", " & random & ", " & d)
   Return res
End Sub
 
Top