Math.Random - WHY isn't it random?

So, I have a table called t. I want to randomly find a number from the table, and then remove that number from the table so the next number can’t be the same number as the first. In total, I have four numbers. But when I run the code, every time I get a 1, 5, 2, 5, which shouldn’t possible because I supposedly removed 5 already from the table . How come it I get this result?

--create the table t = {1, 2, 3, 4, 5, 6, 7, 8, 9} --randomly find 1st number from t then remove number1 = math.random(#t) table.remove(t, number1) print ("number 1 is " .. number1) --randomly find 2nd number from t then remove number2 = math.random(#t) table.remove(t, number2) print ("number 2 is " .. number2) --randomly find 3rd number from t then remove number3 = math.random(#t) table.remove(t, number3) print ("number 3 is " .. number3) --randomly find 4th number from t then remove number4 = math.random(#t) table.remove(t, number4) print ("number 4 is " .. number4) --Result: 1 5 2 5 which shouldn't be possible!

You want to seed it with something like this in main.lua to make it random:   math.randomseed(os.time())

There’s a fare number of good discussions about how random number isn’t quite random unless you seed it properly…  You may want to google “math.random isn’t random in lua”

Naomi

Edit:  Also see:  http://www.lua.org/manual/5.1/manual.html

If I do os.time, how will the numbers be different? They are calculated at the same time, right?

You might want to look into what os.time returns…

http://lua-users.org/wiki/DateAndTime

http://www.lua.org/manual/5.1/manual.html#pdf-os.time

I feel it would be quite a coincidence if two people happen to launch the app and call os.time exactly at the same moment…  You might want to try printing out the os.time at runtime and see what you get.

Naomi

I don’t think your code does what you think it does. Using #t only returns the length of the table t. So the first run through you get math.random(9) (any number 1-9), then the next one is math.random(8) (any number 1-8), the next math.random(7) (any number 1-7) and finally math.random(6) (any number 1-6). So you could in a horrible bad random reality get 5 each time.

I think what your wanting it this:

--create the table t = {1, 2, 3, 4, 5, 6, 7, 8, 9} --randomly find 1st number from t then remove number1 = t[math.random(#t)] table.remove(t, number1) print ("number 1 is " .. number1) --randomly find 2nd number from t then remove number2 = t[math.random(#t)] table.remove(t, number2) print ("number 2 is " .. number2) --randomly find 3rd number from t then remove number3 = t[math.random(#t)] table.remove(t, number3) print ("number 3 is " .. number3) --randomly find 4th number from t then remove number4 = t[math.random(#t)] table.remove(t, number4) print ("number 4 is " .. number4) --Result: 1 5 2 5 which shouldn't be possible!

This pulls the number represented in the t table and places it in the variables. This way the 5th table entry will be different each time even if you randomly get 5 from math.random.

Thanks.

You want to seed it with something like this in main.lua to make it random:   math.randomseed(os.time())

There’s a fare number of good discussions about how random number isn’t quite random unless you seed it properly…  You may want to google “math.random isn’t random in lua”

Naomi

Edit:  Also see:  http://www.lua.org/manual/5.1/manual.html

If I do os.time, how will the numbers be different? They are calculated at the same time, right?

You might want to look into what os.time returns…

http://lua-users.org/wiki/DateAndTime

http://www.lua.org/manual/5.1/manual.html#pdf-os.time

I feel it would be quite a coincidence if two people happen to launch the app and call os.time exactly at the same moment…  You might want to try printing out the os.time at runtime and see what you get.

Naomi

I don’t think your code does what you think it does. Using #t only returns the length of the table t. So the first run through you get math.random(9) (any number 1-9), then the next one is math.random(8) (any number 1-8), the next math.random(7) (any number 1-7) and finally math.random(6) (any number 1-6). So you could in a horrible bad random reality get 5 each time.

I think what your wanting it this:

--create the table t = {1, 2, 3, 4, 5, 6, 7, 8, 9} --randomly find 1st number from t then remove number1 = t[math.random(#t)] table.remove(t, number1) print ("number 1 is " .. number1) --randomly find 2nd number from t then remove number2 = t[math.random(#t)] table.remove(t, number2) print ("number 2 is " .. number2) --randomly find 3rd number from t then remove number3 = t[math.random(#t)] table.remove(t, number3) print ("number 3 is " .. number3) --randomly find 4th number from t then remove number4 = t[math.random(#t)] table.remove(t, number4) print ("number 4 is " .. number4) --Result: 1 5 2 5 which shouldn't be possible!

This pulls the number represented in the t table and places it in the variables. This way the 5th table entry will be different each time even if you randomly get 5 from math.random.

Thanks.