Wish Rnd max inclusive

MarkusR

Well-Known Member
Licensed User
Longtime User
why is max exclusive at Rnd?

if me say min 1 and max 10 i would expect a range from 1 to 10

just found out that my max value was not used because and i saw a remark in this command.
 

JordiCP

Expert
Licensed User
Longtime User
(I guess) it is because the original Java (java.util.Random) function already works like this. The function
B4X:
int nextInt(int bound);
returns an integer between 0 and bound-1
In order to keep things coherent, the B4A/J function Rnd(min,max) adds 'min' to the result of the above function.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
Rnd2 :)
 

stevel05

Expert
Licensed User
Longtime User
B4X:
'Returns a random integer between MinVal (inclusive) and MaxVal (inclusive)
Public Sub RndInclusive(MinVal As Int,MaxVal As Int) As Int
    Return Rnd(MinVal,MaxVal + 1)
End Sub
 

MarkusR

Well-Known Member
Licensed User
Longtime User
it was not a personal wish.
i think it should be for all in core library.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
I think that adding a Rnd2 method is not a good idea and will not make things less confusing.
it will help understanding the source code if you see Rnd2(1,100) you would think its from 1 to 100. nobody will think its 1 to 99.

but if we need this random from - to we have it and do not need to copy / paste functions.
if we using a shared code modul for all projects we have a problem to share code snippets.
if we like to zip a project folder as backup from windows explorer the external file will be missing.
because of backward compatible its ok to let the Rnd untouched.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
haha ,
u used exact this
Index = rnd (0, List1.Size-1)
because the parameter name was min and max.

for a list i prefer 1 to count but it is as it is. (same for chars in strings^^)
 

LucaMs

Expert
Licensed User
Longtime User
haha ,
u used exact this
Index = rnd (0, List1.Size-1)
because the parameter name was min and max.

for a list i prefer 1 to count but it is as it is. (same for chars in strings^^)

I'm sorry, I did not understand (I hope because of my poor English :p).

I do not use:
Index = rnd (0, List1.Size-1)

but

Index = rnd (0, List1.Size)

because Arrays, Lists and other have base index 0.

Even with strings:

upload_2018-9-4_18-14-14.png


So even to get a single char I should use rnd(0, MyString.Length).
 

MarkusR

Well-Known Member
Licensed User
Longtime User
LucaMs said:
I think it's better this way.
i had understood you but i did not agree "it's better this way".
this is ok or i get headache at reading code.
0 to List1.Count-1
1 to List1.Count
i like to have Option Base 1 (especially for strings)
 
Top