RandomUtil.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4983 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2008-03-28 21:02:07 +00:00
parent c7126f07d5
commit 5b29ca6a24
+40
View File
@@ -0,0 +1,40 @@
//
// $Id$
package com.threerings.util {
/**
* Random Random utilities.
*/
public class RandomUtil
{
/**
* Return the picked index from a weighted list.
*
* @param weights an Array containing Numbers, ints, or uints. All values should be
* non-negative.
* @param a random function that returns (0 <= value < 1), if null then Math.random() will
* be used.
*/
public static function getWeightedIndex (weights :Array, randomFn :Function = null) :int
{
var sum :Number = 0;
for each (var n :Number in weights) {
sum += n;
}
if (sum < 0) {
return -1;
}
var pick :Number = ((randomFn == null) ? Math.random() : randomFn()) * sum;
for (var ii :int = 0; ii < weights.length; ii++) {
pick -= Number(weights[ii]);
if (pick < 0) {
return ii;
}
}
// since we're dealing with floats, it's possible that a rounding error left us here
return 0; // TODO: largest weighted? Re-pick?
}
}
}