jeudi 19 mars 2020

Divide a number n into x random parts such that sum of x parts is equal to number n [closed]

Here n is the number, p is number of parts:

let n = 100;
let p = 11;
let obj = [];
console.log(Math.round(n/p))
while (p != 0){

  let value = Math.round(Math.random() * n);
  obj.push(value);
  n -= value;
  p--;
}

console.log(obj);

This is the output : [ 68, 18, 1, 5, 0, 6, 2, 0, 0, 0, 0 ]




Aucun commentaire:

Enregistrer un commentaire