Math Random But Always Spawn Side By Side?

Hi guys, i wonder how to spawn objects totally random when there are 2-3 different objects coming down altogether?

basically here’s how my script looks like:

[code]–Spawn powerup and bomb
if (level >= 2) then
if(math.random(1,10) == 1) then
powerupspawn()
end
end

if (level == 2) then
if(math.random(1,5) == 1) then
bombspawn()
end
end

– POWERUP SCRIPT
function powerupspawn( objectType, x, y )
powerup = display.newImage(“power-up.jpg”)
powerup.name = “powerup”
powerup.x = math.random(0,330)
powerup.y = -100
powerup.rotation = 10
physics.addBody(powerup, { density=2.0, friction=0.5, bounce=0.3})
localGroup:insert(powerup)
end


– BOMB SCRIPT
function bombspawn( objectType, x, y )
bomb = display.newImage(“ranjau.png”)
bomb.name = “ranjau”
bomb.x = math.random(0,330)
bomb.y = -100
bomb.rotation = 10
physics.addBody(bomb, { density=2.0, friction=0.5, bounce=0.3})
localGroup:insert(bomb)
end
[/code]

with the above script, i wonder why every time the bomb and powerup spawn, they always go side by side? With math.random 0,330 i believe the chance for them to spawn far away one to another should be big but i notice when they spawn together they always spawn very near one to another. Do i need to use another code other than “math.random”?
[import]uid: 114765 topic_id: 21822 reply_id: 321822[/import]

Hi @brad2000xi, math.random needs seeding. This blog post explain why & how very well:

http://hellomobiledevworld.blogspot.com/2011/08/using-mathrandom-in-corona-sdk.html

Naomi [import]uid: 67217 topic_id: 21822 reply_id: 86659[/import]

hi there the code from the link helps but not ultimately helps

i added this at the top of my main.lua

math.randomseed(os.time()) math.random() math.random()

well now the bomb and powerup (sometimes) spawn separately but there are times when they are still spawn side by side. Why is this? Why sometimes they spawn really randomly and at the other time they spawm side by side? Do you have a fix for this? [import]uid: 114765 topic_id: 21822 reply_id: 87105[/import]

Hi Brad,
Try the following code and see if that may help you…:slight_smile:
It should give you a better result as far as random numbers
you could change it up a little with different numbers…
I just used os.time() / mRand(6, 330)…I just used…
(6, 330) as a divide by number…:wink: (6 instead of 0 tho…no
errors that way)…divide by zero error…have fun…:wink:

The functions cached at the beginning of your .lua file
will help optimize the speed …a little anyway…:slight_smile:

-- These two functions cached at the top of your .lua file  
mRand = math.random; -- without the (), this caches the math.random() function  
mRandSeed = math.randomseed; -- same here for faster access  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*--  
--\*\* put this together in your code  
mRandSeed( os.time() );  
powerup.x = mRand(0,330);  
--\*\* put this also together in your code  
mRandSeed( os.time() / mRand(6 , 330)); -- a different result  
bomb.x = mRand(0,330);  
  

Hope that helps sort it a little…

Best Regards,

Larry
[import]uid: 107633 topic_id: 21822 reply_id: 87124[/import]