1
votes

I have looked up several tutorials on how to generate random numbers with lua, each said to use math.random(), so I did. however, every time I use it I get the same number every time, I have tried rewriting the code, and I always get the lowest possible number. I even included a random seed based on the OS time. code below.

require "math"
math.randomseed(os.time())
num = math.random(0,10)
print(num)
1
Your code produces different numbers in the Lua demo at lua.org/demo.html. Comment the require line to run it there. - lhf
it might be the program I am using. it is Lua Development tools using eclipse - DragonProgram
You may have to wait one second between runs to see different numbers. - lhf
that's the thing, it ALWAYS gives me the same number, always the lowest one possible. - DragonProgram
@DragonProgram Try doing this: local seed = os.time() print(seed) math.randomseed(seed). Is seed the same every time? That being, is os.time() returning the same number every time? - NetherGranite

1 Answers

0
votes

I'm using the random function like this:

math.randomseed(os.time())
num = math.random() and math.random() and math.random() and math.random(0, 10)

This is working fine. An other option would be to improve the built-in random function, described here.