简体   繁体   English

在数组内随机划分一个值

[英]Randomly divide a value inside an array

I'm testing how to calculate the amount of loss a group of receivables has when divided in a number of installments. 我正在测试如何将一组应收款分成几批来计算损失额。 I have 10.000 receivables and I need to divide them randomly in a range from 1 to 12 installments, and for each value of installment I have a certain percentage of loss. 我有10.000应收款,我需要将它们随机分为1到12批分期付款,对于每期分期付款,我都有一定百分比的损失。

I've created an array to hold the installments and their values [loss percentage, quantity of receivables divided in that number of installments, installment group loss sum] to populate values [1,2] afterwards. 我创建了一个数组来保存分期付款及其值[损失百分比,应收帐款的数量除以该分期付款的数量,分期付款组的损失总额],然后填充值[1,2]。

var instllmtGrp = {
     x1: [.10,0,0]
    ,x2: [.08,0,0]
    ,x3: [.06,0,0]
    ,x4: [.04,0,0]
    ,x5: [.03,0,0]
    ,x6: [.02,0,0]
    ,x7: [.01,0,0]
    ,x8: [.01,0,0]
    ,x9: [.01,0,0]
    ,x10: [.01,0,0]
    ,x11: [.01,0,0]
    ,x12: [.01,0,0]
};

When I try to create an array with 12 elements (ie 1/12 installments) and set a random value within the range from 1 to 10.000 to each element, I end up with a lot more than 10.000 values in total, since the math's assign a number from 1/10.000 for each array element. 当我尝试创建一个包含12个元素的数组(即1/12分期付款)并为每个元素设置一个介于1到10.000范围内的随机值时,由于数学的赋值,最终我得到的总数超过10.000每个数组元素的数字从1 / 10.000开始。

var parcelas = Array.from({length: 12}, () => Math.floor(Math.random() * (10000 - 1 + 1)) + 1);

Is there a way to set a limit to the sum of the values of an array? 有没有办法设置一个数组的值的总和的限制? Or to assign the random values without exceeding the 10.000? 还是要分配不超过10.000的随机值?

Create the values in a loop. 循环创建值。 After you create each value, you subtract it from the limit on the total. 创建每个值后,请从总数限制中减去它。

 var limit = 10000; var parcelas = []; for (var i = 0; i < 11; i++) { var rand = Math.floor(Math.random() * (limit - 1)) + 1; parcelas.push(rand); limit -= rand; } parcelas.push(limit); console.log(JSON.stringify(parcelas)); console.log('Sum = ', parcelas.reduce((a, b) => a + b, 0)); 

You stated that you wanted random numbers adding up to 10000. In your array and in the existing answer these "random" numbers tend to be descending in value. 您说过,您希望随机数加起来为10000。在您的数组和现有答案中,这些“随机”数的值趋于下降。 In case that is not desired, here is a way to get random (except for the fudge factor on the last element) numbers adding up to 10000, and don't have a descending upper limit. 如果不希望出现这种情况,可以采用这种方法来获得随机数(最后一个元素上的软糖因子除外),这些数字的总和为10000,并且没有上限。

  var parcelas = []; var totalAmount = 10000; var sum = 0; var testSum = 0; for (var i = 0; i < 12; i++) { var val = Math.random(); sum += val; parcelas.push(val); } for (var i = 0; i < 11; i++) { parcelas[i] = Math.round(parcelas[i] * totalAmount / sum); testSum += parcelas[i]; } parcelas[11] = totalAmount - testSum; testSum += parcelas[11]; console.log(parcelas.toString()); console.log("Sum: " + testSum); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM