Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 439
Salman Nazir Avatar asked Feb 17 '16 11:02

Salman Nazir


People also ask

What does math randomseed do Lua?

random , math. randomseed. math. random() generates pseudo-random numbers uniformly distributed.

How does Lua math random work?

Lua random is one of the mathematical concept and it's a library function that can be used for to generate the pseudo-random numbers and it can be called in different ways like the method called without arguments and it will be return with pseudo-random real type of numbers with some uniform distributions time ...

What is the difference between random and random seed?

In this case, random is actually pseudo-random. Given a seed, it will generate numbers with an equal distribution. But with the same seed, it will generate the same number sequence every time. If you want it to change, you'll have to change your seed.

What does math random seed do?

It's a function that creates random numbers from a seed. It's just math. random() but instead of giving it a range of values you give it a seed value. If you give it the same seed value it returns the same number.


1 Answers

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.

like image 163
lhf Avatar answered Oct 09 '22 15:10

lhf