Android Question RND

Discussion in 'Android Questions' started by timwil, Mar 3, 2015.

  1. timwil

    timwil Active Member Licensed User

    This may appear to be a bit strange .....but here we go


    Rnd (Min As Int, Max As Int) As Int
    Returns a random integer between Min (inclusive) and Max (exclusive).

    I assume that inclusive means that the number will be included in the final random number generated. Does exclusive mean that it will NOT?

    Dim xc As Int
    Dim xx As Int
    Dim Shuffel As List



    For xc = 1 To 6
    BonusLine(xc) = ""

    xx = 1

    Do While Shuffel.Size > 1
    xc = Rnd(0,Shuffel.Size -1)
    BonusLine(xx) = Shuffel.Get(xc)
    xx = xx + 1

    BonusLine(6) = Shuffel.Get(0)

    BonusLine(6) is ALWAYS the same as if that is not an option in the random number generation

    Is that so?
  2. DonManfred

    DonManfred Expert Licensed User

    1. Please use CODE tags when posting code here. BTW: Code tags are not using < or >. They need [ and ]!

    to generate a rnd from 1 to 100
    you need to call
    dim r as int = RND(1,101)
    btw: Due to this the command
    xc = Rnd(0,Shuffel.Size -1)
    ' should be
    xc = Rnd(0,Shuffel.Size)
    Erel likes this.
  3. Troberg

    Troberg Well-Known Member Licensed User

    This is probably down to tradition. Random numbers in programming languages has always been like that, either as in B4A, or simply returning a float number >=0 and <1.

    I think the reason stems from the latter case. To, for example, simulate an ordinary six-sided die, you would do a Int(Rnd()*6)+1. If Rnd actually could produce a 1, we would have a tiny, tiny chance of coming up 7 on our six sided die. Now, the casinos wouldn't like that...

    However, as B4A has it, I don't seen that reasoning working, and it would probably be better to have it inclusive (albeit, at this point, too late to change...). But, if you don't like it, it's easy to do your own Rnd:

    Sub InclusiveRnd(Min as int, Max as int) as int
    Return Rnd(MinMax+1)
    End Sub
  4. timwil

    timwil Active Member Licensed User


    That explains why the last "Random" number is always the SAME!
  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