Controlling a random number generator

For my game I use a simple random number generator to spawn enemies:

local temp temp = math.random(1, 4)

But some enemies are easier to avoid then others, so the game might be easy to one person but hard to the other, how can I create a random number generator that generates the same random numbers for each sequence. Any tips would be very helpful. Thank you.

For example, level1.lua generates 1,4,2,4,1,2,3,3,2,1,4, when you return I want the exact same sequence.

local temp

temp = math.random(1, 16)
if temp>10 then --11-16 then
print(“this guy is hard”)
elseif temp<=10 and temp>4 then
print(“medium”)
else
print(“too easy”)
end

Just refreshed the page hold on

This tells me what the random number is, but how can I make it generate the same sequence every time.

Hi.

You might try my MWC plugin, which allows you to supply your own seeds ( w and z ) for each generator you launch. You can also query a generator’s current state. This is simply the updated w - z pair, so you can clone or later rewind the generator using it. (See some of the code toward the bottom here, for instance.)

What this also means is that you could print the  w - z  pair at each step. If you find a sequence of enemies you like, hard-code the  w - z  pair (just before it began) into your program somewhere, to be used to seed the generator when you load the level. Sort of time-consuming to find good seeds, but unfortunately by its very nature a random number generator will be hard to predict.  :slight_smile:

Alternatively, if you won’t need it for anything else (possible, but puts a sharp limit on what else you can do) you could do something similar, if a bit more manual, with math.random() via math.randomseed().

How can I apply math.randomseed() and make it for spawning my enemies:

temp = math.randomseed(1234) -- I want to make sure all results from this seed are a whole number between 1 and 4, I printed this but I don't know how to make it work

I figured it out thank you all for your help.

For example, level1.lua generates 1,4,2,4,1,2,3,3,2,1,4, when you return I want the exact same sequence.

local temp

temp = math.random(1, 16)
if temp>10 then --11-16 then
print(“this guy is hard”)
elseif temp<=10 and temp>4 then
print(“medium”)
else
print(“too easy”)
end

Just refreshed the page hold on

This tells me what the random number is, but how can I make it generate the same sequence every time.

Hi.

You might try my MWC plugin, which allows you to supply your own seeds ( w and z ) for each generator you launch. You can also query a generator’s current state. This is simply the updated w - z pair, so you can clone or later rewind the generator using it. (See some of the code toward the bottom here, for instance.)

What this also means is that you could print the  w - z  pair at each step. If you find a sequence of enemies you like, hard-code the  w - z  pair (just before it began) into your program somewhere, to be used to seed the generator when you load the level. Sort of time-consuming to find good seeds, but unfortunately by its very nature a random number generator will be hard to predict.  :slight_smile:

Alternatively, if you won’t need it for anything else (possible, but puts a sharp limit on what else you can do) you could do something similar, if a bit more manual, with math.random() via math.randomseed().

How can I apply math.randomseed() and make it for spawning my enemies:

temp = math.randomseed(1234) -- I want to make sure all results from this seed are a whole number between 1 and 4, I printed this but I don't know how to make it work

I figured it out thank you all for your help.