1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Php code to display the probability for the given number

Discussion in 'PHP' started by rishibala143, Dec 3, 2009.

  1. #1
    Hi

    I need to display the probability for the given number. Suppose if the given number is 2. It should display (W - Win and L - Loss)

    W - W
    W - L
    L - W
    L - L

    It should generate the probability dynamically for the given number.I dont know how to do this using php.. Please help...Its Urgent...


    Thanks & Regards
    P.Balakrishnan
     
    Last edited: Dec 3, 2009
    rishibala143, Dec 3, 2009 IP
  2. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    maybe you should explain what exactly "probability for the given number" means, and why exactly you need to find it.
     
    organicCyborg, Dec 3, 2009 IP
  3. rishibala143

    rishibala143 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi

    I need to generate probability for win and loss in round...If there are 2 rounds the probability will be
    W-W
    W-L
    L-W
    L-L

    and if there are three rounds i want to generate the probability dyanamically..I need this for the below query

    SELECT team_id, GROUP_CONCAT( play_result, round_no
    ORDER BY round_no
    SEPARATOR '-' ) AS team_res, SUM( play_total ) AS total
    FROM tbl_play
    GROUP BY team_id
    HAVING team_res = 'W1-W2' || team_res = 'W1-L2' || team_res = 'L1-W2' || team_res = 'L1-L2'
    ORDER BY total DESC

    In this above query the win and loss is hardcoded for two rounds...If there is 3 round
    i have to write like this

    SELECT team_id, GROUP_CONCAT( play_result, round_no
    ORDER BY round_no
    SEPARATOR '-' ) AS team_res, SUM( play_total ) AS total
    FROM tbl_play
    GROUP BY team_id
    HAVING team_res = 'W1-W2-W3' || team_res = 'W1-W2-L3' || team_res = 'W1-L2-W3' etc...

    So only i need to generate the probability of given number(i.e round number.)


    Please help
     
    rishibala143, Dec 3, 2009 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    are you wanting something like this:

    0 L L L L L 5
    1 W L L L L 4
    2 W W L L L 3
    3 W W W L L 2
    4 W W W W L 1
    5 W W W W W 0
     
    shallowink, Dec 4, 2009 IP
  5. rishibala143

    rishibala143 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No not like that..

    For example if a team plays 1 round the probability will be W or L (W for Win and L for Loss)

    if a team plays 2 round the probability will be
    W - W
    W - L
    L - W
    L - L

    if a team plays 3 round the probability will be
    W - W - W
    W - W - L
    W - L - W
    L - W - W
    L - W - L
    W - L - L
    L - L - W
    L - L - L

    I want to print like this for the n number of rounds.
     
    rishibala143, Dec 4, 2009 IP
  6. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I know I've done some code like this before that was very neat and proper, but I can't find it, and it was in C++.

    Curiosity got the best of me, so I wrote some very, very hacked up PHP code that will do it for you.

    
    <?
    print_r(probableOutcomes(4));
    
    function probableOutcomes($num) {
    	$resultArray = array();
    	for ($i = 0; $i < $num; $i++) {
    		$binaryString .= "1";
    	}
    	
    	for ($i = 0; $i <= bindec($binaryString); $i++) {
    		$resultString = str_pad(decbin($i), $num, "0", STR_PAD_LEFT);
    		$resultString = str_replace("0", "W", $resultString);
    		$resultString = str_replace("1", "L", $resultString);
    		
    		array_push($resultArray, $resultString);
    	}
    	
    	return $resultArray;
    }
    
    ?>
    
    Code (markup):
    The above example outputs
    
    Array
    (
        [0] => WWWW
        [1] => WWWL
        [2] => WWLW
        [3] => WWLL
        [4] => WLWW
        [5] => WLWL
        [6] => WLLW
        [7] => WLLL
        [8] => LWWW
        [9] => LWWL
        [10] => LWLW
        [11] => LWLL
        [12] => LLWW
        [13] => LLWL
        [14] => LLLW
        [15] => LLLL
    )
    
    Code (markup):
    I really can't stress how janky the above code may be. I've been up for a while, but it does produce correct output for your situation.
     
    organicCyborg, Dec 4, 2009 IP
  7. rishibala143

    rishibala143 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks a lot...It works....


    Regards
    P.Balakrishnan
     
    rishibala143, Dec 4, 2009 IP
  8. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I wouldn't use that with very large numbers though. You'll almost definitely run out of memory.
     
    organicCyborg, Dec 4, 2009 IP
  9. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #9
    The code takes 2^n as it displays all binary numbers from 0 to 11...ntimes . But there are 2^n numbers to be displayed and therefore i doubt if a solution exists which runs with a lesser running time.
     
    iama_gamer, Dec 4, 2009 IP
  10. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    How is it possible to have WWWW or LLLL or any scenario where every team wins? If they are playing against each other then at least 1 of them has to win or lose depending...

    you could probably use the Binomial Coefficient in some way to calculate this: http://en.wikipedia.org/wiki/Binomial_coefficient
     
    szalinski, Dec 4, 2009 IP
  11. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #11
    It generates all possible outcomes for a single team is how. the other team doesn't come into question.
     
    shallowink, Dec 4, 2009 IP
  12. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #12
    Assume it is a coin toss and Head = W, Tails = L and n is the number of times its tossed. Output is all possible outcomes. This is how i understood
     
    iama_gamer, Dec 4, 2009 IP
  13. Edgar_fox

    Edgar_fox Active Member

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #13
    Just make an array of probabilities
     
    Edgar_fox, Dec 9, 2009 IP