math.random give me back same numbers

Hi every one,

I’m trying to make a function that gave me a random password of 4 number and I did like that:

 

local function generaPW() local r = "" local elem = { "1","2","3","4","5","6","7","8","9","0" } for i=1, 4 do local rnd = math.random(#elem) r = r .. elem[rnd] end return r end pword = generaPW()

But this give me back all times (only in the device) that combination of result:

1st time I run: 1285

2nd time I run: 6317

3th time I run: 7046

4th time I run: 9116

and so go on.

Every time I kill my app and I restart it restart that combination on “random” numbers.

I use the random in an other part of my app and I notice that here too gave me the same combination of random outing.

How that is possible? It’s not really random :confused:

Regards

You need to set a seed for the random number generator at the start of your app - using the current time is good as this will always be different.

[lua]

math.randomseed(os.time())

[/lua]

You need to set a seed for the random number generator at the start of your app - using the current time is good as this will always be different.

[lua]

math.randomseed(os.time())

[/lua]