What is math.randomseed(tick())
? What does it even do…? Please explain.
As the name says it, it sets a seed
It’s good for giving some “true” random numbers
math.randomseed(1)
local r1 = math.random(1, 9999)
local r2 = math.random(1, 9999)
local r3 = math.random(1, 9999)
Every time you run this script, r1 will always be the same as r1, r2 will always be the same as r2 and r3 will also stay r3
What do you mean by it will always be the same as r1, r2, r3? I tried it and it gave me random numbers. If I understood correctly
math.randomseed(1)
local r1 = math.random(1, 9999)
local r2 = math.random(1, 9999)
local r3 = math.random(1, 9999)
print(r1, r2, r3)
Run this many times, and you will see that it gives the same output
yes if the seed is the same then the n-th call of math.random
will always be the same.
Hmm I copied the script and it gave me different numbers
Try pasting it in the command line, and running it multiple times
I’ve tried everything but it keeps printing random numbers.
Before you understand what’s a seed, let’s take a look what does math.random() mean.
math.random()
literally means choosing a random number between 2 known numbers. You may ask how it chooses the number randomly? Through a seed.
Computers can’t just randomly pick a number, they’ll need to rely on something to pick something random. The seed comes in.
math.randomseed()
will affect how the randomization changes. You’ll need to supply a number in the parentheses to affect the randomization.
tick()
returns the total amount of time that’s been elapsed since January 1st, 1970, to your current local time.
So if you do math.randomseed(tick())
without putting it in a while loop, everytime a new server is created or you playtest your game, the seed will be different.
Ohh, so now it works:
while wait() do
math.randomseed(1)
print(math.random(1, 299))
end
Correct, however the seed will not change because you’re only setting it to 1. Just imagine seeds have a lot of wonderful plants, and those plants are the randomization.
So the seeds are global? So if I do seed 1 and then seed 2 then those are gonna be the ones they had?
What I think is that seeds will affect randomizations in whatever thread they’re in. Let’s say you have a script that has a seed and another script has a different seed. The seeds aren’t global and it will only affect whatever randomizations that are in the same thread as them. I’m not sure about this but that’s my opinion.
Whatever wierd means, this is it
math.randomseed(2)
print(math.random(1, 299))
Prints the same thing that the other script did… using seed 2
Uh, I don’t understand quite well. Can you explain your current situation again?
Okay, so try this:
Insert a script
math.randomseed(1)
print(math.random(1, 299))
Then see what it prints. Then, make another script with the same code. Prints the same.
I’m not on PC right now, but it prints the same because you use the same seed.
Ideally, you would want to set a randomseed to something that is not predetermined and variable such as os.time()
or tick()
because it will ensure that you will get a truly “random” sequence of numbers when consecutively calling math.random
. However, because of this behaviour of randomseed, I would avoid using math.random
, as a randomseed
can globally influence the function, making it not so random. Also, even when the randomseed
is not set, the seed is generated randomly, so it produces “random” numbers anyways. For more actually random sequences, I would personally use Random.new
instead.
Edit: I remember that tick()
is being deprecated, please don’t use it for future work.