Page 1 of 1

Probability Generator

Posted: Sat Jun 22, 2013 3:49 pm
by dandymcgee
This is extremely simple, and I'm sure it's been posted elsewhere, but it could prove useful to someone:
bool Chance(int successRate, int attempts)
{
    return (rand() % attempts) < successRate;
}
Chance(1,100) returns true ~1% of the time, false ~99% of the time.

Re: Probability Generator

Posted: Sat Jun 22, 2013 6:13 pm
by Rebornxeno
How does the < successrate part work?

Re: Probability Generator

Posted: Sat Jun 22, 2013 6:19 pm
by dandymcgee
Rebornxeno wrote:How does the < successrate part work?
Chance(1,100) generates a random number from 0 to 99 and returns true if it's less than 1.
Assuming rand() was truly random that would happen exactly 1% of the time. Since it's pseudo-random it happens ~1% of the time.

Re: Probability Generator

Posted: Sun Jun 23, 2013 11:56 am
by Rebornxeno
How does the "< successrate;" part work?

Re: Probability Generator

Posted: Sun Jun 23, 2013 1:07 pm
by dandymcgee
Rebornxeno wrote:How does the "< successrate;" part work?
Chance(1,100) generates a random number from 0 to 99 and returns true if it's less than 1.
Assuming rand() was truly random that would happen exactly 1% of the time. Since it's pseudo-random it happens ~1% of the time.

Re: Probability Generator

Posted: Sun Jun 23, 2013 4:34 pm
by Rebornxeno
Oh it returns true or false. I get it now.