iOS Question Generate a random number between two numbers

Discussion in 'iOS Questions' started by JanPRO, Mar 27, 2015.

  1. JanPRO

    JanPRO Well-Known Member Licensed User

    Hello,

    is there a way in B4i to generate a random number, which is between two numbers?

    Best regards

    JanPRO
     
  2. klaus

    klaus Expert Licensed User

    From the documentation.
    Rnd(Min As Int, Max As Int)
    Min inclusive, Max exclusive.
    Generate a number bertween 1 and 100:
    N = Rnd(1, 101)
     
    Erel likes this.
  3. JanPRO

    JanPRO Well-Known Member Licensed User

    Thanks :)
     
  4. Mark Turney

    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!
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Secure random generator with the code that tests it:
    Code:
    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(-1010)
         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(4As 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
     
    Mark Turney likes this.
  6. Mark Turney

    Mark Turney Active Member Licensed User

    Thanks so much Erel! :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice