# Thread: Probability generator (kinda not really... who knows lol)

1. Web Hosting Master
Join Date
Jun 2004
Location
Bay Area -USA
Posts
1,738

## Probability generator (kinda not really... who knows lol)

Hey im trying to write a php function (or even just find one on the net) for this situation.

I want to be able to print out "hello" 50% of the time, "hello2" 30% of the time, and "hello3" 20% of the time.

I guess it would be a probability function but im not really sure.

Does anyone have any ideas on what I could do. Im having a hard time thinking of how to do it.

2. Web Hosting Evangelist
Join Date
May 2004
Posts
500
You could have an array with "Hello" in it 50 times, "Hello2" in it 30 times and "hello3" in it 20 times, then used the rand() function to pick one at random from the array eg;
PHP Code:
``` \$x = rand(1,100); \$array = array( 0=>'hello', 1=>'hello', ... 50=>'hello2', # etc ); echo \$array[\$x];  ```
It's a pretty long winded way though. There probably is an easier way to do it though.

3. Web Hosting Master
Join Date
Jun 2004
Location
Bay Area -USA
Posts
1,738
thanks.

Is there a more efficient way to do it?

4. Web Hosting Master
Join Date
Jan 2003
Posts
1,715
I was working on something similar this week. Here's how you do it.

PHP Code:
``` // Specify how many of each you want in the pool \$pool = array(   'hello' => 50,   'hello2' => 30,   'hello3' => 20, ); // Start the totaler at 0 \$count = 0; // Take each item in our pool (order doesn't matter) foreach (\$pool as \$key => \$odds) {   // Each round only needs to consider the odds of this one   // against past participants.  New ones will get their own   // chance against the winner of this round.   \$count += \$odds;   // Use mt_rand if available.   // I don't like PHP's rand parameters.  C & Perl's are better   if ( rand(1,\$count) =< \$odds ) {     \$winner = \$key;   } } // You should now have a \$winner, and \$count should // equal the total from the pool  ```

5. Web Hosting Master
Join Date
Jun 2004
Location
Bay Area -USA
Posts
1,738
Thanks. Your idea gave me an idea of how to do it. I still have to test that it comes up the correct amount of times but still works so far.

PHP Code:
``` \$lw1 = .5*100; //weight (this is going to be 50) \$lw2 = .3*100; \$lw3 = .2*100; \$sum = \$lw1 + \$lw2 +\$lw3; //will equal 100 in this case \$rand_num = rand(0, \$sum);// 0-100 switch (\$rand_num){     case \$rand_num >= 0 && \$rand_num <= \$lw1://if between 0 and 50         \$link_text = 'text1'; break;     case \$rand_num > \$lw1 && \$rand_num <= \$lw1+\$lw2: //between 51 and 80         \$link_text = 'text2'; break;     case \$rand_num > \$lw1+\$lw2 && \$rand_num <= \$sum: //between 81 and 100         \$link_text = 'text3';break;     } print(\$link_text);  ```

6. Web Hosting Master
Join Date
Jan 2003
Posts
1,715
The PHP rand function returns min-max inclusive, so your first case has a slight advantage (0-50 is 51 numbers). If you use rand(1, \$count), you get the ratio you want. That does make it difficult to handle a 0-chance case (if you wanted to disable a case, but leave it in the code), which is why I like the perl/C versions better.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•