• 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 Random result from 3 or more different options?

Gasil

Member
Hello, I'm trying to figure out a simple way to to this, but I don't think I could without help.

I have these three outcomes or accidents while performing activities, each one has a different chance to occur: 20%, 30% and 50%, so I want a check to determine which will happen over the other two based on its probability.

How would you code something like that? I have no code whatsoever other than:

GML:
//Check if accident will happen.
roll = irandom(99);
if(roll < BASE_ACCIDENT_CHANCE)
{
    //Check which of the 3 different incidents will happen.
}
Thank you for your suggestions.
 
Well, it's easy enough to simply do another roll right?
Code:
//Check if accident will happen.
var roll = irandom(99);
if(roll < BASE_ACCIDENT_CHANCE)
{
   //Check which of the 3 different incidents will happen.
   var roll = irandom(99);
   if (roll < 20) {
      // 20% chance
   }
   else if (roll < 30) {
      // 30% chance
   }
   else if (roll < 50) {
      // 50% chance
   }
}
 

Gasil

Member
Well, it's easy enough to simply do another roll right?
Code:
//Check if accident will happen.
var roll = irandom(99);
if(roll < BASE_ACCIDENT_CHANCE)
{
   //Check which of the 3 different incidents will happen.
   var roll = irandom(99);
   if (roll < 20) {
      // 20% chance
   }
   else if (roll < 30) {
      // 30% chance
   }
   else if (roll < 50) {
      // 50% chance
   }
}
Thank you, friend, the "cascade" effect was messing me up, I thought in that way one incident would have greater chances to happen than defined, but it's clearer now the way you wrote it. Thanks again, appreciate it.
 

DaveInDev

Member
Hello, I'm trying to figure out a simple way to to this, but I don't think I could without help.

I have these three outcomes or accidents while performing activities, each one has a different chance to occur: 20%, 30% and 50%, so I want a check to determine which will happen over the other two based on its probability.
looking at 20+30+50 = 100, it seems that once you decided there is an accident, we have to choose at least one (like in a wheel of fortune), so I would rather do the check like this :

GML:
   var roll = irandom(99);
   if (roll < 20) {
      // 20% chance
   }
   else if (roll < 20+30) {
      // 30% chance
   }
   else if (roll < 20+30+50) {
      // 50% chance
   }
which can be of course simplified by :
Code:
   var roll = irandom(99);
   if (roll < 20) {
      // 20% chance
   }
   else if (roll < 20+30) {
      // 30% chance
   }
   else  {
      // 50% chance
   }
 
Top