Difference between math.random() and math.randomseed() in Lua

Salman Nazir picture Salman Nazir · Feb 17, 2016 · Viewed 13.4k times · Source

I am working on game using Corona SDK with Lua as Programming Language . While getting random number from table , I am confused to use which one of following ?

math.random()
math.randomseed()

Will any one elaborate the exact difference between them ? So I can use the best one in my case.

Answer

lhf picture lhf · Feb 17, 2016

math.random gives you random numbers. You probably will call this many times in a program that needs random numbers.

math.randomseed determines where to start the sequence of random numbers given by math.random. You probably will call just once in a program that needs random numbers.

It seems to be a common misconception that you need to call math.randomseed before each time you call math.random. This is wrong and will defeat the randomness of math.random.