drewbee Posted May 22, 2006 Share Posted May 22, 2006 Wow... I new I should have paid attention in my statistics class.This is a confusing one (atleast for me). (simplified)A person comes to the site and has a certain percentage chance(x) of doing A, otherwise its BX can be 20% - 90%I am at a complete loss of this one, and I do not even know where to start.Can this be done through plain math, or will I have to keep track by number increments and determine how much one has been displayed over the other? Quote Link to comment https://forums.phpfreaks.com/topic/10156-percentage-chance-of-something-happening/ Share on other sites More sharing options...
AndyB Posted May 22, 2006 Share Posted May 22, 2006 It's not really clear what your question is. The chance of something happening is the same regardless of all previous events, e.g. if you toss a coin and it comes up 'heads' nine times in a row, the probability of it being 'tails' the next time is still 50%. Quote Link to comment https://forums.phpfreaks.com/topic/10156-percentage-chance-of-something-happening/#findComment-37861 Share on other sites More sharing options...
drewbee Posted May 22, 2006 Author Share Posted May 22, 2006 [!--quoteo(post=375946:date=May 21 2006, 10:28 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 21 2006, 10:28 PM) [snapback]375946[/snapback][/div][div class=\'quotemain\'][!--quotec--]It's not really clear what your question is. The chance of something happening is the same regardless of all previous events, e.g. if you toss a coin and it comes up 'heads' nine times in a row, the probability of it being 'tails' the next time is still 50%.[/quote]Apologies. What I am looking for is the certain WEIGHTED percentage chance of something to happen.So it could be a split of 70/30, 50/50, 30/70 etcI have come up with something, although I am not sure how resource intense it is.Basically what I do is populate an array (1-100). Value A will fill the array at the rate of "chance" it has to get selected, then fill the rest of the array with the other option BThen simply select a random number between 1 and 100. Since their will be more A's (if their is a 65% chance of selecting an A, their will be 65 A's, and 35 B's, as such it will be more likely to select an A then a B).[code]$chance = 65;$collection = array();for ($i=1;$i<=100;$i++){ if ($i <= $chance) { $collection[$i] = "A"; } else { $collection[$i] = "B"; }}$select = rand(1,100);echo "This was selected: $collection[$select]";[/code]I ran a script execution time on it ( 0.000231981277466 ), and I think I am more then content with those results.If anyone knows a better way to do this, however, please feel free to let me know.I personally cannot think of another way, but filling a 100 piece array on the fly each and everytime the page loads could be quite a mess after a while. Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/10156-percentage-chance-of-something-happening/#findComment-37863 Share on other sites More sharing options...
AndyB Posted May 22, 2006 Share Posted May 22, 2006 All you want is a probability ... [code]<?php$chance =65; // or whatever number you wantif (rand(1,100)>$chance) { $whatever = "A";} else { $whatever = "B";}echo $whatever;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10156-percentage-chance-of-something-happening/#findComment-37867 Share on other sites More sharing options...
drewbee Posted May 22, 2006 Author Share Posted May 22, 2006 [!--quoteo(post=375952:date=May 21 2006, 10:53 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 21 2006, 10:53 PM) [snapback]375952[/snapback][/div][div class=\'quotemain\'][!--quotec--]All you want is a probability ... [code]<?php$chance =65; // or whatever number you wantif (rand(1,100)>$chance) { $whatever = "A";} else { $whatever = "B";}echo $whatever;?>[/code][/quote]Yeah, that would make a little more sense, thank you sir :) Quote Link to comment https://forums.phpfreaks.com/topic/10156-percentage-chance-of-something-happening/#findComment-37868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.