Finding the percentage in PHP

Discussion in 'Programming' started by Barti1987, Feb 5, 2007.

  1. #1
    I have three variables=chances:

    
    $first = '34';
    
    $second = '22';
    
    $third = '44';
    
    PHP:
    I want one of the above three to be selected based on their chance (percentage), e.g.: $first comes on 34% of times, $second 22% and $third 44% of times.

    Any ideas on how to do it?

    Thanks,

    Peace,
     
    Barti1987, Feb 5, 2007 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Firstly, just a quick hint: if you're using numbers, you shouldn't put quotes around them. Although PHP will *generally* realise that you're using them as numbers it doesn't always work and will assume it's a string. So if you're using strings, use quotes, if you're using numbers, don't use quotes.

    Now... Is the existence of the $first / $second / $third variables required, or are you just using them for trying to find a 'random' variable? I mean, as you have stated the question it doesn't make a whole lot of sense: you want to pick a variable based on the percentage value that that variable has. Then what? You have a variable with it's percentage value... what are you going to do with that??

    Anyway, if I wanted to pick a random value based on percentage, this is what I would do:

    
    $chances = array( 34, 22, 44 );
    $total_chances = array_sum( $chances );
    $random_value = rand( 1, $total_chances );
    
    $current_counter = 0;
    $random_index = -1;
    foreach( $chances as $key => $value ) {
        $current_counter += $value;
        if ( $current_counter >= $random_value ) {
            $random_index = $key;
            break;
        }
    }
    
    PHP:
    Something like that, anyway. At the end of the function, your $random_index will be 1, 2 or 3 depending on the chance of each of the relevant indices.

    I haven't tested it and there may be a few little problems, but you should able to get the drift.
     
    TwistMyArm, Feb 5, 2007 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Thanks for the tip.

    The above three variables WILL be given, they would range from 0 -> 100.


    Exactly what I want to do. I have three people, A, B and C. Each person has an X chance of winning. When the page loads, I will compute the percentage and report which person won, A, B or C. Sorry for not pointing that out clearly firsthand.

    Thanks in advance for the code, it is appreciated taking your time to help. The problem is finding which person was the winner.

    Peace,
     
    Barti1987, Feb 5, 2007 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hmm... OK, so excuse my ignorance but even in your original approach didn't you still have the problem of 'who won'? How would you have approached that originally?

    Having your variables like you did, somehow you would still have to correlate that with a list of users, just as you would have to do with the code I gave you... it's up to you, really, to decide how you want to match them...
     
    TwistMyArm, Feb 5, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Thanks TwistMyArm for your help.

    I have finally got an easier solutions:


    
    $first_range = range(1, $first);
    $second_range = range ($first+1, $first+$second);
    $third_range = range ($first+$second+1, 100);
    
    PHP:
    Then I would rand for a number, each person would have the specified percentage as a rate.

    Peace,
     
    Barti1987, Feb 6, 2007 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK, whatever. I don't think you understood where I was going though... even with that code, you STILL have to relate the range or whatever with the person.

    It still needs to be correlated somehow, just as how my 'random person' has to be correlated with a person.

    Like I say, whatever. I'm glad your happy with your result.
     
    TwistMyArm, Feb 7, 2007 IP