Jump to content

Recommended Posts

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 B

X 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?
Link to comment
https://forums.phpfreaks.com/topic/10156-percentage-chance-of-something-happening/
Share on other sites

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%.
[!--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 etc

I 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 B

Then 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?
[!--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 want
if (rand(1,100)>$chance) {
    $whatever = "A";
} else {
    $whatever = "B";
}
echo $whatever;
?>[/code]
[/quote]

Yeah, that would make a little more sense, thank you sir :)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.