Android Question RND

timwil

Active Member
Licensed User
Longtime User
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

Shuffel.Add("25")
Shuffel.Add("200")
Shuffel.Add("50")
Shuffel.Add("75")
Shuffel.Add("100")
Shuffel.Add("5")

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?
 

DonManfred

Expert
Licensed User
Longtime User
1. Please use CODE tags when posting code here. BTW: Code tags are not using < or >. They need [ and ]!
Does exclusive mean that it will NOT?
correct.

to generate a rnd from 1 to 100
you need to call
B4X:
dim r as int = RND(1,101)

btw: Due to this the command
B4X:
xc = Rnd(0,Shuffel.Size -1)
' should be
xc = Rnd(0,Shuffel.Size)
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime 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:

B4X:
Sub InclusiveRnd(Min as int, Max as int) as int
  Return Rnd(Min, Max+1)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…