✓ Accepted answer
True randomness is not possible from a computational perspective[^1] -- you can do things that make numbers look random, but the next on is always based on the previous ones[^2]. Such a generator is called a pseudo random number generator (PRNG) because the numbers it makes aren't truly random. The data that the PRNG uses to make numbers is called the seed.
If you use math.randomseed, you can determine what sequence of numbers I'm going to get out of math.random() by setting the seed.
math.randomseed(1)
print( math.random() )
print( math.random() )
print( math.random() )
print("")
math.randomseed(1)
print( math.random() )
print( math.random() )
print( math.random() )
Essentially, math.randomseed let's you set the source of randomness, and math.random let's you get from the source of randomness.
There are a few things to note:
This is a global change -- all scripts will be affected. You generally shouldn't use this more than once, at the very beginning
You will get the same sequence of numbers out given the same seed on the same device[^3] -- the way math.random() works is up to the device running it.
You shouldn't usually have to care about calling this. I believe that ROBLOX automatically seeds math.random() with the time online (it does not in Studio, however).
The results of math.random() immediately after calling math.randomseed() might not be very "good". This is because the seed number you picked isn't the result of the processed going on behind-the-scenes, so it might not quite be settled where you get a good distribution of numbers. It's a good idea to use a for loop to just grab 5 to 100 random numbers to make sure it's usable.
The input to math.randomseed should be an integer. The fractional part of the number you put in is thrown out.
[^1]: Whether or not there's a such thing as a "good enough" random number generator is an open question, which has some useful theoretical consequences.
[^2]:You can use empirical sources like measurement errors and environmental noise to take advantage of our inherently unpredictable universe -- however, you can't instruct a computer to simulate that, because then it's no longer unpredictable!
[^3]: The math.random function is a wrapper around C's rand function, and similar math.randomseed is a wrapper around C's srand function.
2 comments
This question is a Duplicated Question, please use the search tab first before asking.
Yeah, don't read the small note I put... It definitely will not tell you why I asked this question... /sarc