percent chance~better way?

Talk about making games.

percent chance~better way?

Postby jimmynewguy » Tue Jul 21, 2009 5:55 pm

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?
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: percent chance~better way?

Postby DST » Wed Jul 22, 2009 6:40 am

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.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: percent chance~better way?

Postby Fuzzy » Fri Jul 24, 2009 2:43 am

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
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: percent chance~better way?

Postby Bee-Ant » Sun Aug 16, 2009 6:08 pm

Why dont you just use the random code directly instead of using unecesary function?
Code: Select all
int i=rand(101);
if(i<=chance){};
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest