increasing math.random value over time

How can i increasing the value of a math.random  over time?  

So i got this line of code: 

 TEST:setLinearVelocity(0, math.random(100, 150))

And like to increase  the (100, 150) by steps of 10 so : 

(100, 150) - (110, 160) - (120, 170) - (130, 180) - … and so on.

What is the best way of doing this? 

Kevin-

function NewRange (first, last, step) local count = last - first return function() local result = math.random(first, first + count) first = first + step return result end end local Velocity = NewRange(100, 150, 10)

Not tested, but that should give you something like it, that you can reuse. Then just call as Velocity() each time.

That is not working but can i do something like this ?

testa = 100 testb = 150 badclouts:setLinearVelocity(0, math.random(.. testa, .. testb)) -- Drop down local function speatTimer()    testa = testa+1    testb = testb+1    --print("testa " .. testa)    --print("testb " .. testb) end local mainTimer = timer.performWithDelay( 1000, speatTimer, 500 )

PS: I do get a ‘unexpected symbol’ error on this code.

Kevin-

 but can i do something like this ?

Well, that’s up to you.  :slight_smile: The unexpected symbol is just your two “…” symbols in the math.random() calls (copy-paste error).

As for my example, I should have said something like “Then call as badclouts:setLinearVelocity(0, Velocity()) each time”.

Hi StarCrunch,

Removing the “…” works :smiley: thanks. anyway is you way better than my way of doing it or dos it not matter? 

Kevin-

Hi. It probably doesn’t matter. Obviously you have a better idea what you’re doing.

If you had several objects independently updating, then you might want to do something like I did.

one more thing when i use this : 

tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3, 0)

it selects 1 between 9/50 (and stays that value) but i want it to refresh this  value every time, any idea how to fix this ?

kevin-

For that, don’t pass the 0 as the last parameter:

tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3) 

Call it the same way at the end of spawnBC3 (let it overwrite the handle, since the old timer’s done).

why i do :

local badclout3 = {}  local bad3Group = display.newGroup() local function spawnBC3()    local badclouts3 = display.newImage("BCloud3.png")    badclouts3.x = math.random(0, \_W)    badclouts3.name = "badclout3"    physics.addBody(badclouts3, {bounce=0, filter=b})    badclouts3.bodyType = "kinematic"    badclouts3.isSensor = true    badclouts3.y = math.random(-100, -50)    badclouts3.index = #badclout3 + 1    bad3Group:insert(badclouts3)    badclouts3.rotation = math.random(-30,30) -- Rotate the object    badclouts3:setLinearVelocity(0, math.random(speeda, speedb)) -- Drop down    badclout3[badclouts3.index] = badclouts3    print("test a " .. spawna)    print("test b " .. spawnb)    print("test speed a " .. speeda)    print("test speed b " .. speedb) return badclouts3  tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3) local mainTimertest = timer.performWithDelay( 100, tmrSpawn, 500 )  

it runes one time

Add the “tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3)” line before “return badclouts3” in spawnBC3 (so it’s called once outside, and the rest of the times after each timeout).

(Also, the mainTimerTest line seems a little suspect… it might be interpreted it as a table listener, but I wouldn’t think it would do anything.)

StarCrunch you are my hero :smiley: Thank you!!

Kevin- 

function NewRange (first, last, step) local count = last - first return function() local result = math.random(first, first + count) first = first + step return result end end local Velocity = NewRange(100, 150, 10)

Not tested, but that should give you something like it, that you can reuse. Then just call as Velocity() each time.

That is not working but can i do something like this ?

testa = 100 testb = 150 badclouts:setLinearVelocity(0, math.random(.. testa, .. testb)) -- Drop down local function speatTimer()    testa = testa+1    testb = testb+1    --print("testa " .. testa)    --print("testb " .. testb) end local mainTimer = timer.performWithDelay( 1000, speatTimer, 500 )

PS: I do get a ‘unexpected symbol’ error on this code.

Kevin-

 but can i do something like this ?

Well, that’s up to you.  :slight_smile: The unexpected symbol is just your two “…” symbols in the math.random() calls (copy-paste error).

As for my example, I should have said something like “Then call as badclouts:setLinearVelocity(0, Velocity()) each time”.

Hi StarCrunch,

Removing the “…” works :smiley: thanks. anyway is you way better than my way of doing it or dos it not matter? 

Kevin-

Hi. It probably doesn’t matter. Obviously you have a better idea what you’re doing.

If you had several objects independently updating, then you might want to do something like I did.

one more thing when i use this : 

tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3, 0)

it selects 1 between 9/50 (and stays that value) but i want it to refresh this  value every time, any idea how to fix this ?

kevin-

For that, don’t pass the 0 as the last parameter:

tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3) 

Call it the same way at the end of spawnBC3 (let it overwrite the handle, since the old timer’s done).

why i do :

local badclout3 = {}  local bad3Group = display.newGroup() local function spawnBC3()    local badclouts3 = display.newImage("BCloud3.png")    badclouts3.x = math.random(0, \_W)    badclouts3.name = "badclout3"    physics.addBody(badclouts3, {bounce=0, filter=b})    badclouts3.bodyType = "kinematic"    badclouts3.isSensor = true    badclouts3.y = math.random(-100, -50)    badclouts3.index = #badclout3 + 1    bad3Group:insert(badclouts3)    badclouts3.rotation = math.random(-30,30) -- Rotate the object    badclouts3:setLinearVelocity(0, math.random(speeda, speedb)) -- Drop down    badclout3[badclouts3.index] = badclouts3    print("test a " .. spawna)    print("test b " .. spawnb)    print("test speed a " .. speeda)    print("test speed b " .. speedb) return badclouts3  tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3) local mainTimertest = timer.performWithDelay( 100, tmrSpawn, 500 )  

it runes one time

Add the “tmrSpawn = timer.performWithDelay(math.random(9, 50), spawnBC3)” line before “return badclouts3” in spawnBC3 (so it’s called once outside, and the rest of the times after each timeout).

(Also, the mainTimerTest line seems a little suspect… it might be interpreted it as a table listener, but I wouldn’t think it would do anything.)