# Using math.randomseed()

I was told you could use math.randomseed() to make math.random() repeat its results when using the same number each time in math.randomseed().

However it doesn’t seem to be working or maybe I’m doing it wrong. Simple test:
[lua]lua_run_cl math.randomseed(1234) print( math.random() )
[/lua]

The number generated each time is different even tho Google searches tell me they should be the same.

``````

] lua_run_cl math.randomseed(1234)  print( math.random() )
0.13995788445692
] lua_run_cl math.randomseed(1234)  print( math.random() )
0.66310617389447
] lua_run_cl math.randomseed(1234)  print( math.random() )
0.79793694875942

``````

Anyone know how to use it so that math.random() (does it also work on math.Rand?) will generate the same number or sequence of numbers more then once?

Errrmm, Seed’s description

“Description:
Seeds the random number generator. Use this if random values seem to be too uniform.” Plus that if your trying to generate the same number more then once then it wouldn’t be math.random…

[editline]12:09AM[/editline]

math.Rand returns fractional values

[editline]12:10AM[/editline]

math.random(1,1) a number between 1 and 1 equals…1?

Lol, just realized why it failed, math.random() with no arguments creates a float…a float between 0 and 1.
You tried to seed it for a number, not between 0 and 1.

[lua]
math.randomseed(1234) print( math.random(1,2000) )
[/lua]

try that

[editline]12:29AM[/editline]

or if you did want a float do

[lua]math.randomseed(1234) print( math.Rand(1,2000) ) [/lua]

I’ve already tried stuff like that, it doesn’t work:

Example:

``````

] lua_run_cl math.randomseed(1234)  print( math.random(1,2000) )
1633
] lua_run_cl math.randomseed(1234)  print( math.random(1,2000) )
1532
] lua_run_cl math.randomseed(1234)  print( math.random(1,2000) )
1152

``````

I see no reason for it not working. Lol well, That’s Garry’s Mod Lua for you.

:derp:

[lua]function math.RandomSequence(length, min, max)
local length = length or 1 – Set the length to 1 if no arguments have been passed.
local sequence = {} – Create the table to hold the sequence.
local index = 0 – Set the position index to 0.
for i = 1, length do – Generate the random sequence.
if min then
if max then
sequence[ i ] = math.random(min, max)
else
sequence[ i ] = math.random(min)
end
end
end
return function()
index = index%length + 1 – Increase the index by 1, wrapping it if it exceeds length.
return sequence[ index ] – Return the next number in the sequence.
end
end[/lua]

math.RandomSequence(length, min, max) generates a random sequence that is length long and returns a function, that when called will return the next number in that sequence. When it reaches the end of the sequence, It will start from the beginning again repeating the same sequence. The sequence will contain integers between min and max, if you pass only the min argument to the function the sequence will contain integers from 1 to min.

Example:

[lua]local sequence = math.RandomSequence(12, 1000) – Generate a random sequence from 1 to 1000 that is 12 long.
for i=1, 50 do – Loop 50 times
print(sequence()) – Print the next number in the sequence.
end[/lua]

Outputs:

``````
841
395
784
799
912
198
336
769
278
554
478
629
841
395
784
799
912
198
336
769
278
554
478
629
841
395
784
799
912
198
336
769
278
554
478
629
841
395
784
799
912
198
336
769
278
554
478
629
841
395

``````

As you can see, every 12 numbers the sequence resets.

[editline]10:52AM[/editline]

Passing a length of 1 will return a function that always returns the same random number.

That wouldn’t work for what I’m doing. The length is unknown until the server side code figures out how many times a while loop ran. Then the client side needs to generate the same random numbers the server side did.

Using math.randomseed should be able to do this however it seems to be broken in gmod since examples from lua tutorial sites don’t work in gmod.

In that case why don’t you just send those numbers to the client.