Aaron
Aaron

Reputation: 1051

How can I generate unique random numbers in quick succession?

I have a function that is called three times in quick succession, and it needs to generate a pseudorandom integer between 1 and 6 on each pass. However I can't manage to get enough entropy out of the function.

I've tried seeding math.randomseed() with all of the following, but there's never enough variation to affect the outcome.

os.time()
tonumber(tostring(os.time()):reverse():sub(1,6))
socket.gettime() * 1000

I've also tried this snippet, but every time my application runs, it generates the same pattern of numbers in the same order. I need different(ish) numbers every time my application runs.

Any suggestions?

Upvotes: 2

Views: 403

Answers (1)

Aaron
Aaron

Reputation: 1051

Bah, I needed another zero when multiplying socket.gettime(). Multiplied by 10000 there is sufficient distance between the numbers to give me a good enough seed.

Upvotes: 3

Related Questions