getChance() - Event Chance Percentage

How to find or create sprites, graphics, sounds and music.

getChance() - Event Chance Percentage

Postby GEuser » Tue May 27, 2014 5:52 pm

SCREENSHOTS
Scrshot_Chance.png

DOWNLOAD
getChance.zip
(32.51 KiB) Downloaded 180 times

INTRODUCTION

Wanted to create a function to test whether a chance event with a given percentage success rate occurs or fails every time the function is run. Wanted it to be more true to real life. Where, for example, if something has a chance of happening 1 in 4 times, in real life it does not always happen once every four counts but averages out to 1 in 4 times over all (e.g. 4 counts= no, 16 counts=4, 36 counts=11, 100 counts = 25...).

The method I've used seems to work partially and is a compromise over using the more complicated poisson distribution stuff. See what you guys think.

DESCRIPTION

int getChance (double percSuccess)

getChance.png

The function generates a real random number between 0.0 and 100.0 and tests it against a low band and high band range based on a percentage success passed in via the function. If random number falls within either the low or high band, it returns a 1. Indicating the chance event has occurred. Otherwise returns a 0 for a failed chance event.

USAGE

To run a code for an event with a success rate chance of 27.25% use:-
Code: Select all
if (getChance(27.25))
{
   // your code to do something
}



GLOBAL CODE
Code: Select all
int getChance(double percSuccess)
{
    int chance=0;
    double randNum=rand(100.0);
    double lowBand=percSuccess/2.0;
    double highBand=100.0-(percSuccess/2.0);
 
    if (percSuccess<0.0) { percSuccess=0.0; }
    if (percSuccess>100.0) { percSuccess=100.0; }
 
    if(((randNum>=0.0)&&(randNum<=lowBand))||((randNum>=highBand)&&(randNum<=100.0)))
    {
        chance=1;
    }
 
    return chance;
}


Feel free to play with the code and let me know if you get it to work better (i.e. more true to real life).
GEuser
 
Posts: 204
Joined: Thu Jan 05, 2012 3:08 pm
Score: 19 Give a positive score

Re: getChance() - Event Chance Percentage

Postby skydereign » Tue May 27, 2014 6:09 pm

Maybe I'm not understanding what you are doing, but if you are going for realism wouldn't the rand function suffice?
Code: Select all
// returns  one success_rate percent of the time, otherwise 0 on miss
int chance_hit (double success_rate)
{
    return (rand(100) < success_rate);
}

So if you do chance_hit(10), it will only return true 1/10th of the time. But because of the rand it doesn't guarantee every 1 in 10 will be a hit. It could very well miss 300 times in a row, but it is highly unlikely. If though you run that simulation thousands of time it will prove to be the proper percentage of hits.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: getChance() - Event Chance Percentage

Postby GEuser » Tue May 27, 2014 9:22 pm

@ Sky

Haha! I've done this in a spreadsheet (x30,000) with your rand() and my getChance() way. What I've noticed is when using whole percentages rand() seems slightly better but getChance is slightly better when using fractional percentages. So, they are more or less equivalent codes with yours being alot more short and efficient. Hehehe, so much for that, I tried atleast. The rand function does indeed suffice. Should have gone to specsavers!

Thanks again skydereign.
GEuser
 
Posts: 204
Joined: Thu Jan 05, 2012 3:08 pm
Score: 19 Give a positive score

Re: getChance() - Event Chance Percentage

Postby knucklecrunchgames » Wed May 28, 2014 8:48 pm

wat :?: :?: :?:
User avatar
knucklecrunchgames
 
Posts: 1071
Joined: Wed Nov 21, 2012 8:01 pm
Location: In gameEditor.exe
Score: 17 Give a positive score


Return to Game Resources

Who is online

Users browsing this forum: No registered users and 1 guest

cron