Math.random() is different for every client, even though I used the same math.randomseed()

Greetings everyone,

I have a script that looks like this:

math.randomseed(1)

while wait(1) do
	local rNumber = math.random(1,2)
	print(rNumber)
end

The problem is, that if I run this with multiple clients, the outputs still vary even though all clients have the same randomseed.

Does anyone know what I did wrong?

Thanks in advance.

Is this the only script calling math.random? Because all scripts in your game use the same state for math.random, calling math.random in one script will affect the output of other scripts using math.random. To avoid this issue, you could use the Random type with the same seed. Each Random object has a separate state, so you can use the same seed to generate the same random numbers on all clients.

3 Likes

The script is client sided, so every client calls math.random().

What I’m trying to archive in the end, is that I have random generation system chunks. This all has to be client-sided to prevent server memory issues.
Although, I need every client to generate the same map using random chunks from a chunk folder. Do you have any other solutions to solve this issue?

Here’s an example of your original code rewritten to use Random:

local random = Random.new(1)

while wait(1) do
	local rNumber = random:NextInteger(1, 2)
	print(rNumber)
end
4 Likes

Thank you so much! That worked.