Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

sound() breaks math.random()

edited October 2011 in Bugs Posts: 447

this prints the same number over and over. sound() is probably resetting the random seed which is not a desirable side effect

function draw()





  • edited October 2011 Posts: 447

    by the way, I don't think math.randomseed() is working either. Below prints out values which are roughly increasing with the seed. This was how I was going to get around the problem above, but didn't work. I can't think of any ways to generate random numbers now if I'm using sounds :(


    seed = math.floor(DeltaTime*100000)



  • JohnJohn Admin Mod
    edited October 2011 Posts: 643

    We are looking into the issue. Delta time is just the time between the last frame and the current frame so that would give you constant results from math.random. ElapsedTime wont work much better. For now just try the following:

        rnum = math.random(1000000000)




  • Posts: 447

    Thanks, that works. Here's another hack that seems to be ok:

    count = 0

    function random()

    count = count + 1
    v = math.random()*10000
    v = v - math.floor(v)


  • Posts: 152

    Hi Codeans,

    After banging my head against random number problems with math.random() returning the same value over and over I found this thread...

    I think this bug has reappeared in Codea 2.0...after disabling my sound() calls my random numbers work again...

    Anybody seen the same thing?


  • Posts: 2,048

    @brookesi, I am not seeing it. I tried the following code with both the new sound effects and the old type, and math.random() worked fine.

    function draw()
        sound(SOUND_PICKUP, 40207)
        print(math.random())    -- Still prints out a random number
  • edited May 2014 Posts: 152

    Hi @JakAttak,

    I have managed to reproduce it:

    function setup()
        print("Hello World!")    
        local rs = os.time()
        print("RS", rs)
        local rr = math.random(10000000)
        sound("Game Sounds One:Radar", 1.0, 1.5)
        -- Uncomment the below to re-randomise
        print(math.random(120, 150))
        print(math.random(120, 150))
        print(math.random(120, 150))

    Running this 'as is' results in the same numbers being printed over and over (assuming running with > 1 second period as os.time() being used as seed), commenting out the sound() line restores randomness...

    Then, running with the sound() line and the two lines uncommented 'restores' random behaviour...


  • Posts: 2,048

    @Brookesi, now I see what you mean. Each call returns a different number, but every time you run it they return the same ones in order (120, 124, 143?). Weird bug, but it seems you found a workaround?

  • Posts: 152

    Hi @JakAttack, thanks for confirming ;) Yep, the suggested fix above sorts it out, the problem manifested in my game as at certain points I create a Drone object and calculate a 'random' path between buildings for my drones to fly along and they all ended up sitting on top of each other!

Sign In or Register to comment.