sound() breaks math.random()

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

function draw()

sound(SOUND_EXPLODE,1)

print(math.random())

end

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 :frowning:

sound(SOUND_EXPLODE,1)

seed = math.floor(DeltaTime*100000)

math.randomseed(seed)

print(seed,math.random())

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)

sound(SOUND_EXPLODE,1)

math.randomseed(rnum)

print(math.random())

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

count = 0

function random()

count = count + 1

math.randomseed(DeltaTime*100000+count)

v = math.random()*10000

v = v - math.floor(v)

return(v)

end

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?

Cheers,
Brookesi

@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

end

Hi @JakAttak,

I have managed to reproduce it:

function setup()

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

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…

Brookesi

@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?

Hi @JakAttack, thanks for confirming :wink: 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!