math.random() bug!

Hi.

I found a confusing bug today.

i was testing an app and the return random number i had was always the same in my device. to test if was some other part of my code i made a new code from a new project in corona (blank page)

i’ve made this simple code:

local centerX=display.contentWidth\*.5 local centerY=display.contentHeight\*.5 local number=math.random(1,10) local options={ text=number, fontSize=14, x=centerX, y=centerY } local text=display.newText(options)

if you start the app it should give you a random numbers from 1 to 10. it works on simulator 

and on a samsung device (android 4.2.2) i had to test. but on my device (HTC 10 -  android 6.01) it will give always number 10. i even shutdown the device and started again…still gives me, always 10. i changed the random values…it will give me always the highest value.

please test this simple code on your devices to see if there are more devices affected.

hope corona will check this. don’t know if they can replicate the problem without a htc 10 but the problem exist, i’ve one to prove it. don’t know if the problem is from HTC or from Corona.

btw, i’m using latest build 2017.3030

regards,

Carlos.

if i insert this line before i create the random number it will work on my device (HTC 10):

math.randomseed( os.time() )

So my new question is why i don’t need this line on other devices and on my i need it.

why math.random don’t behave the same on all devices? its a feature or a bug?

i even tried to math.random() that should give me a float number from 0 to 1…then i multiply it with 10 and round that result to give me an integer…still didn’t work on my device, only with randomseed worked. still confused.

Try checking out this tutorial…

https://coronalabs.com/blog/2015/12/22/tutorial-understanding-random-numbers-in-corona/

You’ll need to random seed first - 

math.randomseed( os.time() )

 

thanks, graham07 for you reply. i read that tutorial before i posted here my post, but still don’t know it works without math.randomseed in some devices and others it will work only with it.

Some OS’s might seed math.random() for you, others not so.

It’s always best to make sure to do it yourself.

Rob

if i insert this line before i create the random number it will work on my device (HTC 10):

math.randomseed( os.time() )

So my new question is why i don’t need this line on other devices and on my i need it.

why math.random don’t behave the same on all devices? its a feature or a bug?

i even tried to math.random() that should give me a float number from 0 to 1…then i multiply it with 10 and round that result to give me an integer…still didn’t work on my device, only with randomseed worked. still confused.

Try checking out this tutorial…

https://coronalabs.com/blog/2015/12/22/tutorial-understanding-random-numbers-in-corona/

You’ll need to random seed first - 

math.randomseed( os.time() )

 

thanks, graham07 for you reply. i read that tutorial before i posted here my post, but still don’t know it works without math.randomseed in some devices and others it will work only with it.

Some OS’s might seed math.random() for you, others not so.

It’s always best to make sure to do it yourself.

Rob