# Percentage chance of something happening

4 replies to this topic

### #1 drewbee

drewbee
• Members
• 171 posts
• LocationColumbus, Ohio

Posted 22 May 2006 - 02:12 AM

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?
With C you can shoot yourself in the leg. With C++ you can reuse the bullet.

### #2 AndyB

AndyB
• Staff Alumni
• 5,465 posts
• LocationToronto

Posted 22 May 2006 - 02:28 AM

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%.
Legend has it that reading the manual never killed anyone.
My site

### #3 drewbee

drewbee
• Members
• 171 posts
• LocationColumbus, Ohio

Posted 22 May 2006 - 02:46 AM

[!--quoteo(post=375946:date=May 21 2006, 10:28 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 21 2006, 10:28 PM) [/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).

```\$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]";
```

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?
With C you can shoot yourself in the leg. With C++ you can reuse the bullet.

### #4 AndyB

AndyB
• Staff Alumni
• 5,465 posts
• LocationToronto

Posted 22 May 2006 - 02:53 AM

All you want is a probability ...
```<?php
\$chance =65; // or whatever number you want
if (rand(1,100)>\$chance) {
\$whatever = "A";
} else {
\$whatever = "B";
}
echo \$whatever;
?>```

Legend has it that reading the manual never killed anyone.
My site

### #5 drewbee

drewbee
• Members
• 171 posts
• LocationColumbus, Ohio

Posted 22 May 2006 - 02:57 AM

[!--quoteo(post=375952:date=May 21 2006, 10:53 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 21 2006, 10:53 PM) [/div][div class=\'quotemain\'][!--quotec--]
All you want is a probability ...
```<?php
\$chance =65; // or whatever number you want
if (rand(1,100)>\$chance) {
\$whatever = "A";
} else {
\$whatever = "B";
}
echo \$whatever;
?>```
[/quote]

Yeah, that would make a little more sense, thank you sir
With C you can shoot yourself in the leg. With C++ you can reuse the bullet.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users