What is math.randomseed()

I’m trying to understand math.randomseed() I see people use it everywhere for minigames but I don’t understand it. After a month I decided to make a choice to post here to see if anyone can explain it simply.

12 Likes

This question has been asked before, I recommend reading these threads for a better explanation. In simple terms, computers don’t have the ability to generate “real” random numbers, so they take seeds.

Seeds are randomized to make the computer give pseudo-randomness, even though it’s been predefined already. That’s why

math.randomseed(tick())

generates a random seed and is used to mimic true randomness.

10 Likes

So it just generates a random number?

I read like 10 topics about math.randomseed() but I can’t figure out why my brain don’t understsand it

1 Like

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.

It’s like saying 2+3 always equals 5. If you gave math.randomseed() a value of 5 it will always return the same value when you give it 5.

Computers can’t generate true random numbers so instead they always rely on some function to generate their “random numbers”. All math.randomseed() is, is a big complex function that takes one input and gives one output.

11 Likes

So basically math.randomseed(5) will generate a random seed and it will always return that number if you call it?

1 Like

Scripts like this are the reason you should avoid math.random altogether. In a perfect world, code like this would only run once when the game starts up. In reality it might run every time a script is cloned, or even in a loop somewhere if you inserted an unfortunate free model.

Any script can call math.randomseed and mess up the global seed for all scripts, resulting in not-so-random values. You should use Random.new() in cases where randomness is important.

13 Likes

No, 5 is the random seed. But Yes, calling math.randomseed(5) will always return the same value.

1 Like

Imagine creating a unique seed like a plant seed based on unique conditions you gave it such as watering amount and weather.

If you were to plant that seed again with the exact same conditions, it will grow up to be the exact same plant again. Ignore heredity and genetics

That is how you can visualize math.randomseed(), where your input determines a pseudo-randomized seed that you use for random functions.

9 Likes

This is great information, thank you!

So basically reading all of your guy’s comments math.randomseed(5) will return 5 when you call it and 5 is the seed. Why doesn’t it print anything?

local RandomSeed = math.randomseed(5)

print(RandomSeed)

math.randomseed doesn’t actually return anything, it a void function. All that calling it does is it influence math.random() in your script

math.randomseed(1)
print(math.random(1, 100)) -- prints 97

math.randomseed(2)
print(math.random(1, 100)) -- prints 26

math.randomseed(1)
print(math.random(1, 100)) -- prints 97

The reason that you see a lot of people doing math.randomseed(tick()) is because since the value of tick() always counts up, and if you put it in the beginning of your script, every time the game starts and the script runs and the random seed is changed, you get different numbers from math.random() every new time the game starts.

If you didn’t put math.randomseed(tick()) in the beginning of the script, every new time you start the game, math.random(1, 3) will always return the same number.

( tick() returns the amount of seconds since the start of 1970 )

12 Likes

so if I call math.randomseed(1) in the beginning of my script how do you use it if you want to choose a random map?

Calling math.randomseed() is meant to change how the math object generates pseudo-random numbers. You get random numbers by calling math.random(number min, number max). I also don’t recommend calling math.randomseed() like you posted above as it would cause the same map to be picked over and over, just let roblox do the job for you.

can you give me an example of when to use math.randomseed()?

math.randomseed() shouldn’t really be used unless you have a very specific need to set the seed for random number generation. If you really had to, you could use the function to make sure the seed was different. In general: there really isn’t a scenario I imagine where you need to call this function. Just use the Random class if you absolutely need to change the seed and such.

1 Like

in math.randomseed(5) 5 is the seed?

That would be a correct assumption.

1 Like

If you don’t know what randomseed does, you definitely don’t need it.

2 Likes

math.random is a function that Just Works. It has no prior setup, so you can just type it where you need to use it. Having to look up how to use the Random datatype and assign a variable beforehand every time, and also needing to learn how to use NextInteger and NextNumber along with the purpose of the Clone function should one need it, is simply not an elegant solution in contrast to the ease of using the original function.
</rant>

That said, if it’s important as you mention then it does make sense to learn to use the new API.

2 Likes