加起来一组量的阵列中创建的数字 [英] Create numbers within an array that add up to a set amount

查看:103
本文介绍了加起来一组量的阵列中创建的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的PHP - 编程一般。所以基本上我需要做到的是,创建的 X 的数组数量(随机生成),其价值加起来的 N 的:

I'm fairly new to PHP - programming in general. So basically what I need to accomplish is, create an array of x amount of numbers (created randomly) whose value add up to n:

让我们说,我要创建4个数字加起来为30。我只需要在第一随机数据集。在4和30在这里是将由用户设置的变量。

Let's say, I have to create 4 numbers that add up to 30. I just need the first random dataset. The 4 and 30 here are variables which will be set by the user.

基本上类似

x = amount of numbers;
n = sum of all x's combined;

// create x random numbers which all add up to n;

$row = array(5, 7, 10, 8) // these add up to 30

此外,没有允许重复,所有数字必须是正整数。

Also, no duplicates are allowed and all numbers have to be positive integers.

我需要一个数组中的值。我一直在一段时间瞎搞吧,不过,我的知识是相当有限的。任何帮助将大大AP preciated。

I need the values within an array. I have been messing around with it sometime, however, my knowledge is fairly limited. Any help will be greatly appreciated.

推荐答案

首先,这是一个非常酷的问题。我几乎可以肯定,我的方法并不完美,甚至分配数字,但应该比这里的一些其他方法。

First off, this is a really cool problem. I'm almost sure that my approach doesn't even distribute the numbers perfectly, but it should be better than some of the other approaches here.

我决定建立从最低数量达阵(并在年底洗牌它们)。这让我总是选择一个随机的范围内即会产生可有效结果。由于数字始终必须增加,我解决对于确保了有效的解决方案仍然存在(最高可能数目即,如果n = 4和max = 31,如果第一数目被采摘为7,则它不会可以挑选号码大于7,使得4个数字的总和将等于31)。

I decided to build the array from the lowest number up (and shuffle them at the end). This allows me to always choose a random range that will allows yield valid results. Since the numbers must always be increasing, I solved for the highest possible number that ensures that a valid solution still exists (ie, if n=4 and max=31, if the first number was picked to be 7, then it wouldn't be possible to pick numbers greater than 7 such that the sum of 4 numbers would be equal to 31).

$n = 4;
$max = 31;
$array = array();

$current_min = 1;
while( $n > 1 ) {
    //solve for the highest possible number that would allow for $n many random numbers
    $current_max = floor( ($max/$n) - (($n-1)/2) );
    if( $current_max < $current_min ) throw new Exception( "Can't use combination" );
    $new_rand = rand( $current_min, $current_max ); //get a new rand
    $max -= $new_rand; //drop the max
    $current_min = $new_rand + 1; //bump up the new min
    $n--; //drop the n
    $array[] = $new_rand; //add rand to array
}
$array[] = $max; //we know what the last element must be
shuffle( $array );

编辑:对于大值 $ N 你会朝向数组的结尾了很多分组值的结束,因为有一个很好的机会,你会附近得到最大值强制休息是非常接近的一个随机值。一个可能的解决办法是有一个加权兰特,但这已经超出了我。

For large values of $n you'll end up with a lot of grouped values towards the end of the array, since there is a good chance you will get a random value near the max value forcing the rest to be very close together. A possible fix is to have a weighted rand, but that's beyond me.

这篇关于加起来一组量的阵列中创建的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆