Probability Generator

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Locked
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Probability Generator

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Probability Generator

Post by Rebornxeno »

How does the < successrate part work?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Probability Generator

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Probability Generator

Post by Rebornxeno »

How does the "< successrate;" part work?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Probability Generator

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Probability Generator

Post by Rebornxeno »

Oh it returns true or false. I get it now.
Locked