• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Ratio-based chance?

Hello GMC,

Just a quick one. Not sure if I'm missing something here, but is it possible to base chance outcome using a ratio? I have the following variables and logic:

strength = 3
resist = 7

ratio = strength/resist

and this would indicate about a 43% chance (3/7) for "A" happening, and about 57% (1 - (3/7) ) for "B" happening. But what would be the appropriate way to put this in code form?:

I have:

if (ratio)
{show_message("A")}
else
{show_message("B")}

but obviously something is missing so it doesn't work.

Any help appreciated. Thanks.
 

Aajowa

Member
Not sure if this is the 'correct' way, but you might be able to set up the chance via;
GML:
r_strength = irandom_range(0,3) //3 being whatever variable you're using to determine strength
r_resistance = irandom_range(0,7) //7 being determined the same way

if (r_strength>=r_resistance){
//bleep bloop
}else{
//Neep norb
}
 

Roldy

Member
Hello GMC,

Just a quick one. Not sure if I'm missing something here, but is it possible to base chance outcome using a ratio? I have the following variables and logic:

strength = 3
resist = 7

ratio = strength/resist

and this would indicate about a 43% chance (3/7) for "A" happening, and about 57% (1 - (3/7) ) for "B" happening. But what would be the appropriate way to put this in code form?:

I have:

if (ratio)
{show_message("A")}
else
{show_message("B")}

but obviously something is missing so it doesn't work.

Any help appreciated. Thanks.
There would be many ways to do this.

One way would to be to think about pies. e.g. you can think of 3/7ths as a pie cut into seven equal parts and you have 3 of those parts.

If you generated a random number using irandom_range, from 1 part to 7 parts, you could do "A" when the number is 1, 2, or 3 and "B" when the number is 4,5,6,7.

So something like:

GML:
strength = 3;
resist = 7;

// Choose a number between 1 and the value of 'resist' (currently 7)
var _value = irandom_range(1, resist);

if (_value <= strength) {
    show_debug_message("A");
} else {
    show_debug_message("B");
}
 

AceKiron

Member
YellowAfterLife made a blog post about exactly this: https://yal.cc/gamemaker-weighted-choose/
Here's the script given for GMS 2.3+:
GML:
function choose_weighted() {
    var n = 0;
    for (var i = 1; i < argument_count; i += 2) {
        if (argument[i] <= 0) continue;
        n += argument[i];
    }
    
    n = random(n);
    for (var i = 1; i < argument_count; i += 2) {
        if (argument[i] <= 0) continue;
        n -= argument[i];
        if (n < 0) return argument[i - 1];
    }
    
    return argument[0];
}
 

chamaeleon

Member
Under the assumption if strength is greater than or equal to resist it will always be successful, using your formula
GML:
if ( (strength/resist) >= random(1) ) {
    show_message("A");
} else {
    show_message("B");
}
I would hazard a guess there's no limit to number of ways one can solve it as all posts so far indicate.
 
Top