Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the Difference Between Random and RandomSeed?

What is the difference between the two functions, math.random(), and math.randomseed()?

I looked up this wiki article, but it really does not go all that in depth to what randomseed is.

I am wondering how useful it would be to make a thing truly random, rather than me having to do something like;

repeat wait() until tick()%5==0
print(math.random(1,10))

to avoid getting the same number over and over again.

######I am also asking since Sukinahito thought it was a good question to ask for future reference.

So further explanation on the difference of these methods would be helpful, thank you!

2 comments

1

This question is a Duplicated Question, please use the search tab first before asking.

1

Yeah, don't read the small note I put... It definitely will not tell you why I asked this question... /sarc

2 answers

✓ 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() )

--[[Output:
0.0012512588885159
0.56358531449324
0.19330423902097

0.0012512588885159
0.56358531449324
0.19330423902097
]]

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:

  1. This is a global change -- all scripts will be affected. You generally shouldn't use this more than once, at the very beginning

  2. 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.

  3. 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).

  4. 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.

  5. 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.

Definition

math.random is a function that produces pseudo-random numbers based upon two parameters: a seed and the last random number that it produced.

On the other hand, math.randomseed is a function that edits one of the two parameters above; the seed. The first argument to math.randomseed will be the new seed used for generating numbers.

True random

True random is not computationally feasible. Most random function implementations in languages use the technique above where there is a seed that acts as a transformation function applied on the last number the function generated.

Why use math.randomseed if there is no "true" random

If you do not define a new seed with math.randomseed, then all Lua instances will be seeded with 0 by default. This means that you will produce pseudo-random numbers, but the next time you create a Lua instance you will encounter the same numbers again.

You can prevent this by seeding with a number that will vary from instance to instance. Most people will use the tick function to achieve this effect, which is the local time elapsed since the Unix epoch.

Because tick will typically vary from instance to instance, you will get pseudo-random numbers perfect for regular use just by setting math.randomseed.

This discussion is preserved as it was. New answers, comments, and votes are closed.