How to make a "seed" so that the order of randomizing would be the same each run

I want to make a “seed” so that the order of randomizing would be the same
Example:

-- Playtest 1
print(math.random(0, 1)) -- prints 1
print(math.random(0, 1)) -- prints 0
print(math.random(0, 1)) -- prints 0
-- ..and so on..
-- Playtest 2(Same seed)
print(math.random(0, 1)) -- prints 1
print(math.random(0, 1)) -- prints 0
print(math.random(0, 1)) -- prints 0
-- ..and so on..
--Playtest 3(Different seed)
print(math.random(0, 1)) -- prints 0
print(math.random(0, 1)) -- prints 1
print(math.random(0, 1)) -- prints 0
-- ..and so on..

Sorry if I didn’t explain it properly.

I’ve heard about math.randomseed but I don’t really understand it. I tried it, but it isn’t always the same order each test.

Any help would be appreciated.

I mean, math.randomseed is exactly what you need.


I didn’t understand it at first either, but it’s like setting a property! I know it looks like a function but, oh well. I’d like some more info about this:

math.randomseed sets the seed for that current environment, say the whole (Local)Script (if no coroutines are used.)

Oh, I see. How can I do this to all other scripts? Like as if this seed is for the entire game? I’m kinda new to some of these stuff.

The easiest way to do it (I’m gonna assume you’re creating a Doors-like game) is to set a property to workspace ONCE.

-- Server script somewhere, run this once:
workspace:SetAttribute("RandomSeed",12345)
-- Anywhere else
math.randomseed(workspace:GetAttribute("RandomSeed"))
-- math.random code...

It’s important to set the random seed at start of each script that uses it so you maintain the consistent experience.

math.randomseed(12345)
print(math.random())
print(math.random())
print(math.random())

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.