math.probablity

Is there a way to set probablity between two things? Let say I want one photo to pop 80% of the times and another one to pop 20% of the times. Is there a shortcut to do so. I’ve written this code but it’ll be better if there’s a shorter way.


--# Main

--# Main
function setup()
    photo = {}
    photo[1] = readImage("Cargo Bot:Codea Icon")
    photo[2] = readImage("Cargo Bot:Codea Logo")
    t = 5
    i = 1
    previousi = 1
    twop = 0
    onep = 0
    parameter.watch("percenttwo")
end

function draw()
    background(255, 255, 255, 255)
    t = t-1
    if t==0 then
        i = math.random(1,10)
        if i==previousi then t=1 else
            t = 5
            previousi = i
        end
    end
    if i<=8 then
        p = 1
        onep = onep+1
    elseif i>8 then
        p = 2
        twop = twop+1
    end
    percenttwo = (twop/(twop+onep))*100
    sprite(photo[p],WIDTH/2,HEIGHT/2)
end

that’s pretty much the way to go.

You can just use math.random() and then test for 0.8, because math.random generates numbers in the range 0-1.

Okay cool. Then I think I’ll make a class for it.

. @Saurabh I don’t think you need a whole class for one if statement.


    if math.random<.8 then
        photo1()
    else
        photo2()
    end

Yeah your right @Dave1707. I don’t know I just complicate things a lot by adding ( unnecessary )variables. Thanks!!