From 5e710361c44b0b7ce563b9d9721b171c4c5f9b70 Mon Sep 17 00:00:00 2001 From: shish Date: Sat, 14 Jun 2008 18:45:00 +0000 Subject: [PATCH] this is useful git-svn-id: file:///home/shish/svn/shimmie2/trunk@899 7f39781d-f577-437e-ae19-be835c7a54ca --- core/util.inc.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/util.inc.php b/core/util.inc.php index 6a625727..f9a4193f 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -455,6 +455,21 @@ function sanitise_environment() { } } +function weighted_random($weights) { + $total = 0; + foreach($weights as $k => $w) { + $total += $w; + } + + $r = mt_rand(0, $total); + foreach($weights as $k => $w) { + $r -= $w; + if($r <= 0) { + return $k; + } + } +} + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * Event API * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */