  Android Question RND

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

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

Documentation:

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?

<code>
Dim xc As Int
Dim xx As Int
Dim Shuffel As List

Shuffel.Initialize

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

xx = 1

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

BonusLine(6) = Shuffel.Get(0)
</code>

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

Is that so?

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

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

Erel likes this.

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:

Code:
`Sub InclusiveRnd(Min as int, Max as int) as int  Return Rnd(Min, Max+1)End Sub`

4. Thanks

That explains why the last "Random" number is always the SAME!