Page 1 of 1

percent chance~better way?

PostPosted: Tue Jul 21, 2009 5:55 pm
by jimmynewguy
i know how to do rand(5) if(rand== 1){yea} but is there a better way to do percent chance then the way i came up with?

Code: Select all
void chance(int percent)
{
i = round(rand(100));
if(i <= percent)
{
p = i;
}
}

then where ever your action is
Code: Select all
chance(25);
if(i == P)
{
//do stuff
}

i guess this works, but it isn't really 25% it's kinda if the number is less then the number you picked out of a random number ranged from 0 - 100.....there has to be a better way to do this, or should i just go back to the good 'ol fashioned way?

Re: percent chance~better way?

PostPosted: Wed Jul 22, 2009 6:40 am
by DST
Statistically speaking, it is 25%. It should be true 25 percent of the time, on average. The fact that the number might be 99 doesn't affect the odds.

Re: percent chance~better way?

PostPosted: Fri Jul 24, 2009 2:43 am
by Fuzzy
jimmynewguy wrote:i guess this works, but it isn't really 25% it's kinda if the number is less then the number you picked out of a random number ranged from 0 - 100.....there has to be a better way to do this, or should i just go back to the good 'ol fashioned way?


From 0 to 100 (inclusive) is 101 digits with a value of 0.0099 each. 0 to 99 inclusive is exactly 0.01 and so fits percentages.

Rand(100) will get you from 0 to 99. 25% is anything less than 25.. ie 0-24.

If it bugs you, use rand(100)+1 for a range of 1-100

Re: percent chance~better way?

PostPosted: Sun Aug 16, 2009 6:08 pm
by Bee-Ant
Why dont you just use the random code directly instead of using unecesary function?
Code: Select all
int i=rand(101);
if(i<=chance){};