View Full Version : percentage out of 100 with chances

Dan13071992

01-31-2012, 04:21 PM

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:

http://crimewave.360-tactics.co.uk/crimespic.png

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

Keleth

01-31-2012, 04:38 PM

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

$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.

Dan13071992

01-31-2012, 05:13 PM

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

$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?

Keleth

01-31-2012, 05:25 PM

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.

Fou-Lu

01-31-2012, 05:56 PM

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:

$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?

Dan13071992

01-31-2012, 06:57 PM

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:

if ($rand > $successrate)

would infact in my eyes be this:

if ($rand < $successrate)

Keleth

01-31-2012, 07:00 PM

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:

if ($rand > $successrate)

would infact in my eyes be this:

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.

Dan13071992

01-31-2012, 07:02 PM

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?

Fou-Lu

01-31-2012, 07:03 PM

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:

$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.

Keleth

01-31-2012, 07:21 PM

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.

Dan13071992

01-31-2012, 07:38 PM

so something like this:

$chance = explode("-", $fetch->crimechance);

$newchance0 = $chance[0] + $random0;

if ($newchance0 > 100) {

$newchance0 = 100;

}

like that?

Keleth

01-31-2012, 08:08 PM

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

$newchance = $chance[0] + $random > 100?100:$chance[0] + $random.

Dan13071992

01-31-2012, 10:27 PM

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?

Keleth

01-31-2012, 11:01 PM

Yah, its simple

if (x == y) echo 1;

else echo 2;

becomes

echo x == y?1:2

expression?if true:if false

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

BluePanther

02-01-2012, 01:11 AM

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

$newchance = $chance[0] + $random > 100?100:$chance[0] + $random.

Yah, its simple

if (x == y) echo 1;

else echo 2;

becomes

echo x == y?1:2

expression?if true:if false

http://php.net/manual/en/language.operators.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:

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.

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.