Android Question Rnd Functionality?

Computersmith64

Well-Known Member
Licensed User
Longtime User
I'm hoping that Erel will answer this one directly, but thought I'd throw it out there for the general population...

How does the Rnd function, function? Does it just make a direct call to the Java Random method in the Math class, or is it a standalone B4A function? The reason I ask is that I have had some players of my Yahtzee! game question the randomness of the die rolls. I think they are somewhat unfounded, but I'm interested to know exactly how Rnd works...

Thanks - Colin.
 

DonManfred

Expert
Licensed User
Longtime User
Show your code which you think did not work. and tell us what you have expected.
B4X:
Dim zufall As Int = Rnd(1, 101) ' Create randum number from 1 to 100

snap0042.png
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi Manfred,

I think the code works fine, but I'm interested in finding out whether Rnd makes a call to Math.Random, or if it's a core B4A function. The "issues" that 3 or 4 (out of almost 60,000) players have reported is that they think sometimes the rolls are not completely random. I had one user complain that they roll Yahtzee's too often (!?) & another who thinks that a single die throwing up the same number several times in a row is not random.

Fyi - I simply use 2 For loops for the roll sequence. The outer loop (0 - 4) is for each die (there are 5 dice in Yahtzee) & the inner loop (1 - 8) generates a random number between 1 & 6 on each iteration. The number generated on the last iteration of the inner loop is what is assigned to the die specified in the outer loop.

Thanks - Colin.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
yatzee is too often; means all numbers the same? Could be a problem with referencing... Please show the mail dice-routine... specially where the DIMs are and what is done with a calculated number. Placed to an list of diced numbers?
Data put to an list for example are put in the list BY REFERENCE, not BY VALUE. I could imagine this behaviour could be the problem here
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Whatever the method, all computer generated numbers are pseudo-random, besides, even TRUE random numbers often repeat.
Yeah - that's my feeling as well. Especially when the range is only 6 numbers wide. I suppose I could in theory be reducing the randomness by iterating 8 times through a simulated die throw. If you think about it, when you throw a die it probably only normally turns up 2 or 3 sides before it stops rolling (unless you are throwing it down a craps table). The 8 iterations are more about animation than the result, so I suppose I could pick whatever number comes up in the first iteration - but I'm not sure if that would actually make it more random...
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The simplest way is to check: make a small program of random number creation, count how many you get from each number and compare after doing a very big number of cases. The number of all counters should be close.
This is what I got:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim c(6), k As Int
    For i = 0 To 1000000000
        k = Rnd(1,7)
        c(k-1) = c(k-1) + 1
    Next
    For i = 0 To 5
        Log((i+1) & " " & c(i))
    Next
End Sub
Program started.
1 166663561
2 166688873
3 166670692
4 166649104
5 166665642
6 166662129
Program started.
1 166665224
2 166655696
3 166669094
4 166666794
5 166665678
6 166677515

By two results you can see that there is no consistency to one of the numbers to be bigger.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
The simplest way is to check: make a small program of random number creation, count how many you get from each number and compare after doing a very big number of cases. The number of all counters should be close.
This is what I got:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim c(6), k As Int
    For i = 0 To 1000000000
        k = Rnd(1,7)
        c(k-1) = c(k-1) + 1
    Next
    For i = 0 To 5
        Log((i+1) & " " & c(i))
    Next
End Sub



By two results you can see that there is no consistency to one of the numbers to be bigger.
Thanks David,

I wrote a similar test app, however one that uses the same algorithm for die rolls in Yahtzee! & got a similar result. There is some variation in the totals, however it's not large or consistent, so doesn't give me any reason to believe that the results of rolls in the game are not random.

Thanks - Colin.
 
Upvote 0

JohnD

Active Member
Licensed User
Longtime User
I'm hoping that Erel will answer this one directly, but thought I'd throw it out there for the general population...

How does the Rnd function, function? Does it just make a direct call to the Java Random method in the Math class, or is it a standalone B4A function? The reason I ask is that I have had some players of my Yahtzee! game question the randomness of the die rolls. I think they are somewhat unfounded, but I'm interested to know exactly how Rnd works...

Thanks - Colin.

I needed a non-repeating set of 10 random numbers in the range of 0 to 9. I found that the Rnd function not only gave me repeats, but also very frequently gave me back-to-back repeats for a small data set, over a short time interval. I wrote the function to give me the non-repeating set I wanted. What I would do if I were you is take this a step further. Since your range is 1 to 6, I would create 6 non repeating, unique, sets of 6. Then for any 6 consecutive rolls I would use Rnd to get the value at the index that rand produces. First roll Rnd(Inx) first array. Second roll Rnd(Idx) second array...etc. That way you could have a repeat but it is less likely even if Rnd produces the same random number in consecutive rolls.
 
Upvote 0

TomA

Active Member
Licensed User
Longtime User
Bear in mind that, if the numbers are truly random, then sometimes an individual number may repeat several times. On any single dice roll, the odds of any given number showing up are 1 in 6. The probability of any given number showing up on successive rolls (like a series of 1s) can be computed, but it can happen. The odds of any single child born being a boy or a girl is 1 in 2, the probability of successive births being of the same sex becomes smaller and smaller as each child is born, but I know of one family that was made up of 5 boys in a row. In rolling dice playing craps, where a first roll of 7 is a winner, I remember one game where an individual rolled that number 8 times in a row. So in Yahtzee, it is entirely possible for a particular sequence to show up an unusual number of times no matter how random the generated die numbers may be - in a truly random sequence, it can be almost guaranteed to happen once in a while. So on 60,000 downloads, having 3 or 4 raise the issue would seem to me a reasonable number of to have had unusual sequences happen.
 
Upvote 0

JohnD

Active Member
Licensed User
Longtime User
I agree in concept Tom. Your probability claims are 100% correct and very well explained. Now let's talk about computerized implementation. The implementation of Rand functions in computerized applications are almost always based on some kind of polling of the system clock. This action itself takes a degree of randomness away. A degree of sequential, non-random probability is introduced. This is why the Rand function implemented by Java has an unusually high incidence of 2 in a row and 3 in a row repetitions for a small (in numeric range - {1,...6}, {0,...9}, {1,...20}) dataset over a short (less than 1 second) system clock polling period.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I agree in concept Tom. Your probability claims are 100% correct and very well explained. Now let's talk about computerized implementation. The implementation of Rand functions in computerized applications are almost always based on some kind of polling of the system clock. This action itself takes a degree of randomness away. A degree of sequential, non-random probability is introduced. This is why the Rand function implemented by Java has an unusually high incidence of 2 in a row and 3 in a row repetitions for a small (in numeric range - {1,...6}, {0,...9}, {1,...20}) dataset over a short (less than 1 second) system clock polling period.

Which is exactly the result I saw in the test app I wrote. No matter how many cycles I ran, there was a relatively high incidence where there were numbers that came up 2 or 3 times in a row. On a full roll, 5 * 8 iterations (so 40 iterations in total) takes about 1 second to complete, however that includes the time it takes to render each iteration by way of loading a png onto an ImageView.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
In an App i am using a color-changing time with the RND-Function and it works fine:
B4X:
Sub Uhrzeit_Tick
    lblUhrzeit.Text = (DateTime.Time(DateTime.now))
    lblUhrzeit.TextColor = Colors.RGB (Rnd(0,128),Rnd(0,128),Rnd(0,128))
    lblUhrzeit.Invalidate
End Sub
 
Upvote 0

JohnD

Active Member
Licensed User
Longtime User
In an App i am using a color-changing time with the RND-Function and it works fine:
B4X:
Sub Uhrzeit_Tick
    lblUhrzeit.Text = (DateTime.Time(DateTime.now))
    lblUhrzeit.TextColor = Colors.RGB (Rnd(0,128),Rnd(0,128),Rnd(0,128))
    lblUhrzeit.Invalidate
End Sub

My posts described functionality in b4a v3.0. They are a little dated now. :)
 
Upvote 0

bluejay

Active Member
Licensed User
Longtime User
My posts described functionality in b4a v3.0. They are a little dated now. :)
I have B4A v3.82 and confirm the RND function is still broken.

Every app where I have used RND has had this issue of excessive runs of the same result whether is Rnd(0,8) to select random menu transitions to Rnd(0,100) to display random quotations. The user experience is that it does not feel random.

Testing random number generators requires a series of different of tests only one of which is the long run distribution. For example the sequence 111222333444555666 would also pass the distribution test.

While this is clearly an Android/Java issue and nothing to do with B4A, we do need work around. If we want to use it games or large simulations the trick will be to find a random number solution that does not dramatically slow down the generation of random numbers.
 
Upvote 0
Top