random is not random?

I have a timer ‘spawning’ items every second.

The ‘spawning’ functions has this code

local fnRandomBall = function()  
  
counter = math.random(100)  
if counter \> 80 then  
 local rndBall = display.newImage( "ball\_orange.png" )   
 rndBall.x = 40 + math.random( 240 ); rndBall.y = 10  
 physics.addBody( rndBall, { ballBody } )  
 rndBall.myName = "orange"  
 rndBall.isHit = false  
 transition.to(rndBall, {time=ballSpeed, y=360})  
else  
 local rndBall = display.newImage( "ball\_blue.png" )   
 rndBall.x = 40 + math.random( 240 ); rndBall.y = 10  
 physics.addBody( rndBall, { ballBody } )  
 rndBall.myName = "blue"  
 rndBall.isHit = false  
 transition.to(rndBall, {time=ballSpeed, y=360})  
end  
  

* I actually have more than 2 balls, but just for illustration, I’m setting a simple IF/Else.

Every time the game starts, it will always start with blue; even if you retry playing again or if you restart simulator or device. [import]uid: 7856 topic_id: 4648 reply_id: 304648[/import]

Did you use randomseed() to mix up the numbers returned? Typically you pass the time in as the seed in order to always get a different sequence of numbers. [import]uid: 12108 topic_id: 4648 reply_id: 14693[/import]

I have used randomseed and I’m not convinced about the randomness of it. Something that’s supposed to be 50/50 is more like 70/30 after a lot of testing. [import]uid: 10835 topic_id: 4648 reply_id: 14733[/import]

The interesting thing on my random, is that the object always appears on the same X

Not sure, but I really need to be able to make this random…random :slight_smile:
I’m planning on displaying a different message every time a game ends and by the looks of it, it will always be the same message :frowning: [import]uid: 7856 topic_id: 4648 reply_id: 14765[/import]

I have different messages every time my game ends and that works well enough. However I have 2 gameover screens that are supposed to be shown 50/50, yet it’s not really so.

So I’d also love a more random, random function.

Oh yeah, there’s a bug on math.random where the first call will always return the same number. So call it first at the beginning of your code and THEN start to use it. [import]uid: 10835 topic_id: 4648 reply_id: 14767[/import]

Just FYI in case someone found this post.
The fix is here:
http://developer.anscamobile.com/forum/2010/01/05/mathrandom-bug [import]uid: 7856 topic_id: 4648 reply_id: 15669[/import]