Get `n` random values between 2 numbers having average `x`(在具有平均“x的2个数字之间获取“n随机值)
问题描述
我想得到 n
个介于 1 到 5(包括两者)之间的随机数(例如 n=16)(整数),因此平均值为 x.
I want to get n
random numbers(e.g n=16)(whole numbers) between 1 to 5(including both) so that average is x.
x 可以是 (1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5) 之间的任何值.
x can be any value between (1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5).
我正在使用 PHP.
例如假设我的平均 x=3.
e.g. Suppose I have average x= 3.
然后需要 16 个介于 1 到 5 之间的整数(包括两者).比如 (1,5,3,3,3,3,2,4,2,4,1,5,1,5,3,3)
Then required 16 whole numbers between 1 to 5(including both). like (1,5,3,3,3,3,2,4,2,4,1,5,1,5,3,3)
更新:
如果 x=3.5 表示 16 个数字的平均值应该在 3.5 到 4 之间.
如果 x=4 表示 16 个数字的平均值应该在 4 到 4.5 之间
如果 x=5 表示所有数字都是 5
if x=3.5 means average of 16 numbers should be between 3.5 to 4.
and if x=4 means average of 16 numbers should be between 4 to 4.5
and if x=5 means all numbers are 5
推荐答案
这个答案允许目标平均值的任何值(无论 n 是奇数还是偶数),并避免使用递归来优化性能.
This answer allows any value for the target average (regardless of whether n is odd or even) and avoids the use of recursion to optimize performance.
函数
function getRandomNumbersWithAverage($target_average, $n, $min=1, $max=5)
{
if($min>$max) list($min, $max) = array($max, $min);
if($target_average<$min || $target_average>$max) return false;
else if($target_average==$min) return array_fill(0, $n, $min);
else if($target_average==$max) return array_fill(0, $n, $max);
if($n<1) return false;
else if($n==1) return array($target_average);
else
{
$numbers = array();
for($i=0;$i<$n;$i++)
{
$sum = array_sum($numbers);
$average = $i ? $sum/($i+1) : ($min+$max)/2;
$contrived_number = $target_average*($i+1) - $sum;
// Last one must be contrived
if($i==$n-1) $new_number = ceil($contrived_number); // Round up
else
{
// The tolerance gets smaller with iteration
$tolerance = ($max-$min)*(1-($i/($n-1)));
$temp_min = ($contrived_number-$tolerance);
if($temp_min<$min) $temp_min = $min;
$temp_max = ($contrived_number+$tolerance);
if($temp_max>$max) $temp_max = $max;
$new_number = mt_rand($temp_min, $temp_max);
}
if($new_number==0) $new_number = 0; // Handle -0
$numbers[] = $new_number;
}
// Since the numbers get more contrived towards the end, it might be nice to shuffle
shuffle($numbers);
return $numbers;
}
}
示例输出:
getRandomNumbersWithAverage(1, 12)
produced the numbers: (1,1,1,1,1,1,1,1,1,1,1,1) having an average of: 1
getRandomNumbersWithAverage(1.1, 13)
produced the numbers: (1,1,1,1,1,1,1,4,1,1,1,0,1) having an average of: 1.1538461538462
getRandomNumbersWithAverage(2.7, 14)
produced the numbers: (3,3,2,5,1,2,4,3,3,2,3,3,3,1) having an average of: 2.7142857142857
getRandomNumbersWithAverage(2.7, 15)
produced the numbers: (3,3,4,3,4,2,1,1,3,2,4,1,5,1,4) having an average of: 2.7333333333333
getRandomNumbersWithAverage(3.5, 16)
produced the numbers: (5,5,4,3,1,5,5,1,2,5,3,3,4,4,4,2) having an average of: 3.5
getRandomNumbersWithAverage(3.5, 17)
produced the numbers: (5,2,3,5,4,1,2,3,5,4,5,4,2,3,5,3,4) having an average of: 3.5294117647059
getRandomNumbersWithAverage(4, 18)
produced the numbers: (3,5,5,3,5,5,3,4,4,4,5,2,5,1,5,4,5,4) having an average of: 4
getRandomNumbersWithAverage(4.9, 19)
produced the numbers: (5,5,5,5,7,5,5,5,5,6,5,3,5,5,3,5,5,5,5) having an average of: 4.9473684210526
getRandomNumbersWithAverage(5, 20)
produced the numbers: (5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5) having an average of: 5
getRandomNumbersWithAverage(0.5, 10)
does not produce numbers
getRandomNumbersWithAverage(0, 9)
does not produce numbers
getRandomNumbersWithAverage(-1, 8)
does not produce numbers
getRandomNumbersWithAverage(5.5, 7)
does not produce numbers
getRandomNumbersWithAverage(6, 6)
does not produce numbers
getRandomNumbersWithAverage(6, 5, 1, 7)
produced the numbers: (7,7,2,7,7) having an average of: 6
getRandomNumbersWithAverage(6, 5, 1, 6)
produced the numbers: (6,6,6,6,6) having an average of: 6
getRandomNumbersWithAverage(3, 1)
produced the numbers: (3) having an average of: 3
这篇关于在具有平均“x"的2个数字之间获取“n"随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在具有平均“x"的2个数字之间获取“n"随机值
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01