mercredi 5 octobre 2016

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.

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.

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 (i.e. 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.

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?




Aucun commentaire:

Enregistrer un commentaire