dimanche 9 mai 2021

Generate all permutations numbers that add up to four including decimals of .5 and .25 in an array

I am trying to randomly generate rhythms. I need to randomly select an array of numbers that add up to 4, only with whole numbers or with decimals that end in .5 or .25, not including zero. example: [0.5, 2, 1.25, 0.25]. I originally tried to write down all the permutations, but once i decided to include the decimal ones i realized it would take awhile and there would probably be a way to generate them. here was the original code by writing the permutations without decimals.

var permutations = [
  [1, 1, 1, 1],
  [1, 1, 2],
  [1, 2, 1],
  [2, 1, 1],
  [2, 2],
  [3, 1],
  [1, 3],
  [4]
];

let getRandomArray = function() {
  return permutations[Math.floor(Math.random() * permutations.length)];
}

console.log(getRandomArray());



Aucun commentaire:

Enregistrer un commentaire