math.random issue

I have this code:

timer1 = timer.performWithDelay(math.random(500, 3000), generateRandomGood,0)
I want it to generate a certain object every couple of seconds, but I want the interval between the creations be different every time.

with the code above, it generates an objects, and then randomizes a number of seconds between the creations. But the number doesn’t change every time.

so what can I use instead of math.random so the numbers different every time. [import]uid: 104376 topic_id: 22087 reply_id: 322087[/import]

This can be accomplished recursively with this approximation:

[code]
local random = math.random

local function generateRandomGood()
– Generate your goods here

if (continueGeneratingCondition) then
timer.performWithDelay(random(500, 3000), generateRandomGood)
end
end

generateRandomGood() – Initialize generation
[/code] [import]uid: 87138 topic_id: 22087 reply_id: 87784[/import]

It will stay the same this way, you’re right.

You might consider calling the timer once, then onComplete calling it again and so on and so forth. This should make it random each time. [import]uid: 52491 topic_id: 22087 reply_id: 87791[/import]

How do I use onComplete?

[import]uid: 104376 topic_id: 22087 reply_id: 87804[/import]

In this case you could actually do something like this;

[lua]local myRandom = math.random(1000,10000)

local function test ()
myRandom = math.random(1000,10000)
print (myRandom)
timer.performWithDelay(myRandom, test, 1)
end
timer.performWithDelay(myRandom, test, 1)[/lua]

Run that and a random number between 1,000 and 10,000 will be used on the timer each time. So between 1 and 10 seconds. (It will print in the terminal as well.) [import]uid: 52491 topic_id: 22087 reply_id: 87816[/import]

Would I need math.randomseed(os()) in that? [import]uid: 104376 topic_id: 22087 reply_id: 87823[/import]

You’d have it at the start of the file most likely, not within the function. [import]uid: 52491 topic_id: 22087 reply_id: 87990[/import]