Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: percentage out of 100 with chances

1. ## percentage out of 100 with chances

hi guys, i have a piece of code, where I want to do percentages out of 100, so that there is different levels of chances, eg, if there is a chance of passing a test and the percentage is 80% then there is a 80% chance of passing and a 20% chance of failing.

If this just sounds like mumbo jumbo, then hopefully i can describe it a bit better with this picture:

so basically, they have the list of percentages for each crime, this is drawn from the database, and if they select a crime and submit the form, it gets processed, ive got all the submit ect, i just dont know how to work out how to pass/fail due to percentages.

Cheers.

Dan

• You want a random number generator... in PHP, mt_rand is my choice.

PHP Code:
``` \$failLimit = 20;\$chance = mt_rand(0, 100);if (\$chance > \$failLimit) echo "You succeeded at \$crime!";else echo "You failed at \$crime.";  ```
You can, of course, flip the comparator if you wanna check against success instead.

• Originally Posted by Keleth
You want a random number generator... in PHP, mt_rand is my choice.

PHP Code:
``` \$failLimit = 20; \$chance = mt_rand(0, 100); if (\$chance > \$failLimit) echo "You succeeded at \$crime!"; else echo "You failed at \$crime.";  ```
You can, of course, flip the comparator if you wanna check against success instead.
with this though, wouldnt it always pass if the chance is from a database thats say 87% success rate?

• Originally Posted by Dan13071992
with this though, wouldnt it always pass if the chance is from a database thats say 87% success rate?
Not sure I follow?

mt_rand will make a random number between 0 and 100. If you set failLimit at 13 (which means success is 87), and mt_rand returns 14, that means its a success, and will go to the success message. If its 13 or lower, it'll mean its a failure. Like I said, you can flip the greater then and the numbers to base it off success as well.

On second though, you might want to set mt_rand from 1 to 100.. adding 0 means there are 101 numbers, which is not what you want if you are going to have whole number percentages.

• Think outside of the bounds of the hardcoded limitation. The idea is to compare the rate in which a random occurrence may succeed or fail (without going into heavy statistics). The easiest solution is to check a 'failure' / 'success' limit to that of a random roll (think dice).
So you take what you have selected from the database as a success rate of 87%. Using a random generator (which no isn't really random, but the pseudorandom is considered 'good enough' in comparison to actual statistical calculations), you would check if it is literally rolled above or below that number. So with a success rate:
PHP Code:
``` \$rand = mt_rand(1, 100); if (\$rand > \$successrate) // Success rate is your integer; 87 for 87% for example {     // This is a failure. }  ```
As successrate approaches 100, the probability that \$rand is > \$successrate diminishes creating the effect of a higher probability of success.
As successrate approaches 0, the probability that \$rand > \$successrate increases creating the effect of a higher probability of failure.

Does that help describe it?

• yes that does describe it, however what im having problems understanding (this bit may sound very stupid of me) is that if the 87% is the success rate, sure that means that 87 times out of 100 times you will pass, thats how i see it, so if the random number is generated, will 87 percent of the numbers be a success using mt_rand?

thats what im having trouble with, as to me (might be wrong) this:

PHP Code:
``` if (\$rand > \$successrate)  ```
would infact in my eyes be this:

PHP Code:
``` if (\$rand < \$successrate)  ```

• Originally Posted by Dan13071992
yes that does describe it, however what im having problems understanding (this bit may sound very stupid of me) is that if the 87% is the success rate, sure that means that 87 times out of 100 times you will pass, thats how i see it, so if the random number is generated, will 87 percent of the numbers be a success using mt_rand?

thats what im having trouble with, as to me (might be wrong) this:

PHP Code:
``` if (\$rand > \$successrate)  ```
would infact in my eyes be this:

PHP Code:
``` if (\$rand < \$successrate)  ```
Yes. If you're using success rate, it would be lower then. I used failure rate, which is why I used greater then. If its success rate, I would actually use less then or equal to.

• ok thank you sorry thats the bit i was getting so stuck with lol,

i do have another question though,

each time someone does a crime, it will update that crime chance by either 1,2,3, however, how can i stop it going past 100?

• The answer is no. Pseudo random guarantees nothing. I could theoretically roll 100 times and pull the same number 100 out of 100 of those times, which of course indicates that 87% of the time would either pull a 100% or 0% depending on what the number is. That is a property of randomness and without extensive statistical analysis on previous random data, there is no way to generate an actual 87% chance of probability.

For the variables as you see, rename \$rand to \$failure if you desire. The point is that if a number exceeds another number, it is flagged as either successful or failure. Since we are comparing to a success rate, the number greater than the bounds is our probability of failure. The alternative to perceive it:
PHP Code:
``` \$roll = rand(1, 100);\$failureChance = 100 - \$successrate;if (\$roll > \$failureChance){    // This is successful.}  ```
Effectively inverting the success rate into a failure rate, so our roll exceeds that of failure. Both are valid options and ultimately create an identical effect.

• Originally Posted by Dan13071992
each time someone does a crime, it will update that crime chance by either 1,2,3, however, how can i stop it going past 100?
Don't just update by a random number... pull the number, add your random increment, and check if its over 100, and if it is, cap it.

• so something like this:

PHP Code:
``` \$chance = explode("-", \$fetch->crimechance);\$newchance0 = \$chance[0] + \$random0;if (\$newchance0 > 100) {\$newchance0 = 100; }  ```
like that?

• More or less... you can also use the if shorthand for it:

PHP Code:
``` \$newchance = \$chance[0] + \$random > 100?100:\$chance[0] + \$random.  ```

• ive never used a short hand if before, i dont know how to write them, could you inform me more on how they work/ how to write them please, just for future refrence?

• Yah, its simple

PHP Code:
``` if (x == y) echo 1;else echo 2;  ```
becomes

PHP Code:
``` echo x == y?1:2  ```
expression?if true:if false

http://php.net/manual/en/language.op...comparison.php and scroll down to ternary operators. You can even stack them in themselves.

• Originally Posted by Keleth
More or less... you can also use the if shorthand for it:

PHP Code:
``` \$newchance = \$chance[0] + \$random > 100?100:\$chance[0] + \$random.  ```
Originally Posted by Keleth
Yah, its simple

PHP Code:
``` if (x == y) echo 1; else echo 2;  ```
becomes

PHP Code:
``` echo x == y?1:2  ```
expression?if true:if false

http://php.net/manual/en/language.op...comparison.php and scroll down to ternary operators. You can even stack them in themselves.
I just want to mention something on style. I usually write my ternaries with parenthesis:
PHP Code:
``` echo (x == y)?1:2; // instead of echo x == y?1:2;  ```
Just easier to read for me - useful for people new to ternary operators.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•