Random Number is NOT random

Hi,

I am getting the SAME set of number sequence on my Android Samsung Note5 device (Android 7). I will kill and restart the app, and the output is the same.

Tested on Android Samsung S3 (Android 4), and the numbers generated are random all the time. 

On the Windows simulator, the random numbers differs all the time.

In essence, if I call math.random(1,2), I will ALWAYS get 1 in the first call. It is the same if I change it to other variables like math.random(1,10). Same set of results will appear.

for i=1,10 do print( math.random(1,2) ) end Output : 06-30 14:06:54.190 29369 29390 I Corona : 1 06-30 14:06:54.190 29369 29390 I Corona : 1 06-30 14:06:54.190 29369 29390 I Corona : 2 06-30 14:06:54.190 29369 29390 I Corona : 2 06-30 14:06:54.190 29369 29390 I Corona : 2 06-30 14:06:54.190 29369 29390 I Corona : 1 06-30 14:06:54.190 29369 29390 I Corona : 1 06-30 14:06:54.190 29369 29390 I Corona : 1 06-30 14:06:54.190 29369 29390 I Corona : 2 06-30 14:06:54.190 29369 29390 I Corona : 1

do you initialize your app at start using randomize(os_time()) ?

randomize uses a seed number to base the results off.

it is the same unless you specifically ask for a different seed every start, like using the time code above.

the advantage is that it can be easier to error check code by disabling this.

hope it helps

As anaqim states, it’s important to seed your random number generator using math.randomseed() - https://docs.coronalabs.com/api/library/math/randomseed.html

The most common way to do this is to seed it with os.time() so that it’s always a different seed on startup. 

@Graham, thanks for fixing the “off my head” stuff which isnt always syntax accurate. Need to work on that  :wink:

I got it as part of standard startup code for all projects so I seldom type it.

Thanks. That solved the problem. 

CPUs cannot actually generate a random number in the sense that a human can.  They use a fractional value from an internal timer.  As such you can get the same sequence over and over.  Reseeding simply moves you to new place in the same sequence.  So this simulates randomness.

Take a real-world example, in Minecraft, given the same seed value the landscape will always draw identically irrespective of device.

do you initialize your app at start using randomize(os_time()) ?

randomize uses a seed number to base the results off.

it is the same unless you specifically ask for a different seed every start, like using the time code above.

the advantage is that it can be easier to error check code by disabling this.

hope it helps

As anaqim states, it’s important to seed your random number generator using math.randomseed() - https://docs.coronalabs.com/api/library/math/randomseed.html

The most common way to do this is to seed it with os.time() so that it’s always a different seed on startup. 

@Graham, thanks for fixing the “off my head” stuff which isnt always syntax accurate. Need to work on that  :wink:

I got it as part of standard startup code for all projects so I seldom type it.

Thanks. That solved the problem. 

CPUs cannot actually generate a random number in the sense that a human can.  They use a fractional value from an internal timer.  As such you can get the same sequence over and over.  Reseeding simply moves you to new place in the same sequence.  So this simulates randomness.

Take a real-world example, in Minecraft, given the same seed value the landscape will always draw identically irrespective of device.