Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    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
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You want a random number generator... in PHP, mt_rand is my choice.

    PHP Code:
    $failLimit 20;
    $chance mt_rand(0100);

    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.

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Keleth View Post
    You want a random number generator... in PHP, mt_rand is my choice.

    PHP Code:
    $failLimit 20;
    $chance mt_rand(0100);

    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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Dan13071992 View Post
    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.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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(1100);
    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?

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Dan13071992 View Post
    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.

  • #8
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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(1100);
    $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.

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Dan13071992 View Post
    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.

  • #11
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    so something like this:

    PHP Code:
    $chance explode("-"$fetch->crimechance);

    $newchance0 $chance[0] + $random0;

    if (
    $newchance0 100) {
    $newchance0 100

    like that?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    More or less... you can also use the if shorthand for it:

    PHP Code:
    $newchance $chance[0] + $random 100?100:$chance[0] + $random

  • #13
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #14
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yah, its simple

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

    PHP Code:
    echo == y?1:
    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.
    Last edited by Keleth; 01-31-2012 at 10:03 PM.

  • #15
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by Keleth View Post
    More or less... you can also use the if shorthand for it:

    PHP Code:
    $newchance $chance[0] + $random 100?100:$chance[0] + $random
    Quote Originally Posted by Keleth View Post
    Yah, its simple

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

    PHP Code:
    echo == y?1:
    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 (== y)?1:2;
    // instead of
    echo == y?1:2
    Just easier to read for me - useful for people new to ternary operators.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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