bool Chance(int successRate, int attempts) { return (rand() % attempts) < successRate; }Chance(1,100) returns true ~1% of the time, false ~99% of the time.
Probability Generator
Moderator: Coders of Rage
- dandymcgee
- 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
This is extremely simple, and I'm sure it's been posted elsewhere, but it could prove useful to someone:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Thu Jun 23, 2011 11:12 am
Re: Probability Generator
How does the < successrate part work?
- dandymcgee
- 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
Chance(1,100) generates a random number from 0 to 99 and returns true if it's less than 1.Rebornxeno wrote:How does the < successrate part work?
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!
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Thu Jun 23, 2011 11:12 am
Re: Probability Generator
How does the "< successrate;" part work?
- dandymcgee
- 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
Chance(1,100) generates a random number from 0 to 99 and returns true if it's less than 1.Rebornxeno wrote:How does the "< successrate;" part work?
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!
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Thu Jun 23, 2011 11:12 am
Re: Probability Generator
Oh it returns true or false. I get it now.