我知道如何在PHP中生成一个随机数,但是我想要一个介于1-10之间的随机数,但我想要更多的3,4,5,然后是8,9,10.这怎么可能?我会发布我尝试的但老实说,我甚至不知道从哪里开始.
根据@ Allain的回答/链接,我用PHP编写了这个快速功能.如果要使用非整数加权,则必须修改它.
/** * getRandomWeightedElement() * Utility function for getting random values with weighting. * Pass in an associative array, such as array('A'=>5, 'B'=>45, 'C'=>50) * An array like this means that "A" has a 5% chance of being selected, "B" 45%, and "C" 50%. * The return value is the array key, A, B, or C in this case. Note that the values assigned * do not have to be percentages. The values are simply relative to each other. If one value * weight was 2, and the other weight of 1, the value with the weight of 2 has about a 66% * chance of being selected. Also note that weights should be integers. * * @param array $weightedValues */ function getRandomWeightedElement(array $weightedValues) { $rand = mt_rand(1, (int) array_sum($weightedValues)); foreach ($weightedValues as $key => $value) { $rand -= $value; if ($rand <= 0) { return $key; } } }
对于一个有效的随机数,一致地向着标度的一端倾斜:
选择介于0..1之间的连续随机数
提高功率γ,使其偏置.1表示未加权,下限表示更高的数字,反之亦然
缩放到所需范围并舍入为整数
例如.在PHP(未经测试):
function weightedrand($min, $max, $gamma) { $offset= $max-$min+1; return floor($min+pow(lcg_value(), $gamma)*$offset); } echo(weightedrand(1, 10, 1.5));
有一个很好的教程给你.
基本上:
求和所有数字的权重.
选择一个小于该值的随机数
按顺序减去权重,直到结果为负数,如果是,则返回该数字.
对此的天真黑客将是建立一个列表或数组
1,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,6,7,7,7,8,8, 9,9,10,10
然后从中随机选择.
本教程将以PHP的形式引导您完成多个剪切和粘贴解决方案.请注意,由于下面的注释,此例程会稍微修改一下您在该页面上的内容.
从帖子中获取的功能:
/** * weighted_random_simple() * Pick a random item based on weights. * * @param array $values Array of elements to choose from * @param array $weights An array of weights. Weight must be a positive number. * @return mixed Selected element. */ function weighted_random_simple($values, $weights){ $count = count($values); $i = 0; $n = 0; $num = mt_rand(1, array_sum($weights)); while($i < $count){ $n += $weights[$i]; if($n >= $num){ break; } $i++; } return $values[$i]; }