Random functions return same values

Hi All,

My code is below.

result everytime same:

8

3

what is wrong? 

[lua]function createRandom()
return math.random( 1, 13 )
end
a=createRandom()
print(a)

a=createRandom()
print( a)

a=createRandom()
print( a)[/lua]

This normal, and sometimes even wanted behaviour. If you want “true” random (well, it’s always pseudorandom but you get my point) values set the random seed using the system time. Google around and you’ll see what I mean.

how can i generate new numbers ?

The documentation describes how random (and seeding) works

http://docs.coronalabs.com/api/library/math/random.html

http://docs.coronalabs.com/api/library/math/randomseed.html

Make sure to seed the random number generator at the beginning of your code:

math.randomseed( os.time() )

Rob

Hi Rob,

I am trying to 

[lua]for i = 1, 5, 1 do
math.randomseed(os.time())
a= math.random(1,13)
print(a)
end[/lua]

but still same records return.

I solved this problem. No need to new reply. Thank You.

For anyone who stumbles on this in the future, you only call math.randomseed( os.time() ) once near the top of your main.lua before you start using random numbers.

Rob

This normal, and sometimes even wanted behaviour. If you want “true” random (well, it’s always pseudorandom but you get my point) values set the random seed using the system time. Google around and you’ll see what I mean.

how can i generate new numbers ?

The documentation describes how random (and seeding) works

http://docs.coronalabs.com/api/library/math/random.html

http://docs.coronalabs.com/api/library/math/randomseed.html

Make sure to seed the random number generator at the beginning of your code:

math.randomseed( os.time() )

Rob

Hi Rob,

I am trying to 

[lua]for i = 1, 5, 1 do
math.randomseed(os.time())
a= math.random(1,13)
print(a)
end[/lua]

but still same records return.

I solved this problem. No need to new reply. Thank You.

For anyone who stumbles on this in the future, you only call math.randomseed( os.time() ) once near the top of your main.lua before you start using random numbers.

Rob