« PHP function for calculating BMI (Body Mass Index) | Main

12/29/2009

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a010536c4221f970c0120a78998c3970b

Listed below are links to weblogs that reference Weighted Randomization In PHP:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Thanks for the code, exactly what I was looking for! Although there's a tiny problem with it. Your keys and values are in the wrong order. And you never actually used the variable $options ;)

Should be:
$options = array('rain' => 80, 'sun' => 20);
echo weighted_random(array_keys($options), array_values($options));


Thanks for catching that. I updated fixed the array name & I also swapped the function arguments from (values, weights) to (weights, values).

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Bookmark and Share