Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2007, 02:02 PM   PM User | #1
Digger3000
Regular Coder

 
Join Date: Feb 2005
Location: Texas
Posts: 472
Thanks: 1
Thanked 0 Times in 0 Posts
Digger3000 has a little shameless behaviour in the past
Probability

Let's say I have something like this: $colors=array("red", "blue"); And I want to randomly echo one of the things in the array. But let's say I want it to be more likely for it to be red than blue. I guess I could put red into the array twice, but is there a quicker way? I'm very, very lazy.
__________________
If you're reading this, it may already be too late!
Digger3000 is offline   Reply With Quote
Old 07-04-2007, 02:20 PM   PM User | #2
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
The only easy way, without actually writing out the probability equations using PHP code, is to create an array with the correct number of entries with the "weight" you want -
PHP Code:
$colors=array("red""blue"); // gives one out of two chance for red
$colors=array("red""red""blue"); // gives two out of three chance for red
$colors=array("red""red""red""blue"); // gives three out of four chance for red 
You could shorten the data structure to a color/weight pair, but you would need to add code to produce the expanded array with all the entries. To get a computer to randomly pick from the weighted possibilities, you need a "pool" of the available choices to pick from.

You can use the shuffle(...) or array_rand(...) function instead of a random number to actually generate a random entry.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 07-04-2007, 02:44 PM   PM User | #3
Digger3000
Regular Coder

 
Join Date: Feb 2005
Location: Texas
Posts: 472
Thanks: 1
Thanked 0 Times in 0 Posts
Digger3000 has a little shameless behaviour in the past
I made up a solution that seems like it could work. What do you think?

PHP Code:
$colors=array("2|red""1|blue");
$newcolors=array();
foreach(
$colors as $key => $value) {
$split=explode("|"$value);
$probability=$split[0];
$thecolor=$split[1];
$i=1;
while(
$i <= $probability) {
$newcolors[]=$thecolor;
$i++;
}

__________________
If you're reading this, it may already be too late!
Digger3000 is offline   Reply With Quote
Old 07-04-2007, 02:57 PM   PM User | #4
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
Try it and see if it produces the array you expect. That's where writting, testing, and debugging code gets fun, getting code to do what you want.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 07-04-2007, 03:15 PM   PM User | #5
Digger3000
Regular Coder

 
Join Date: Feb 2005
Location: Texas
Posts: 472
Thanks: 1
Thanked 0 Times in 0 Posts
Digger3000 has a little shameless behaviour in the past
I did print_r($newcolors); and got this: "Array ( [0] => Red [1] => Red [2] => Blue )" So it seems to work.
__________________
If you're reading this, it may already be too late!
Digger3000 is offline   Reply With Quote
Old 07-04-2007, 11:42 PM   PM User | #6
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
Just another method you can use if it suits you... it simulates an array with duplicates determining priority instead of actually making one, but I don't know whether it's faster or not.

PHP Code:
function array_weighted_rand($ary)
{
    
# Pick a random index as if the array was arranged so that it actually
    # had more entries of the values with higher probabilities
    # eg. for values = [ red, blue, green]
    #     and probabilities = [ 2, 1, 3]
    # assume something like:
    # 0 => red
    # 1 => red
    # 2 => blue
    # 3 => green
    # 4 => green
    # 5 => green
    
$prob_idx mt_rand(0array_sum(array_values($ary)) - 1);
    
    
# Find where the selected index maps to the real indices by calculating
    # a running sum on the probabilities and returning whenever the index
    # is exceeded.
    
$running_sum 0;
    foreach (
$ary as $value=>$prob)
    {
        
$running_sum += $prob;
        if (
$running_sum $prob_idx)
            return 
$value;
    }
}

$colors = array(
    
'red'   => 2
    
'blue'  => 1
    
'green' => 3
);
$picks = array(
    
'red'   => 0,
    
'blue'  => 0,
    
'green' => 0
);

for (
$idx 0$idx 100000; ++$idx)
{
    ++
$picks[array_weighted_rand($colors)];
}
print_r($picks); 
ralph l mayo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:35 PM.


Advertisement
Log in to turn off these ads.