Skip to content

Instantly share code, notes, and snippets.

@auser
Created April 28, 2018 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save auser/f289e1c4abef6d417060ab69a20cf4de to your computer and use it in GitHub Desktop.
Save auser/f289e1c4abef6d417060ab69a20cf4de to your computer and use it in GitHub Desktop.
const splitTotal = (total, n) => {
let arr = [];
let i = 0;
while (i < n) {
arr.push(Math.floor(Math.random() * total) + 1)
i++;
}
const sorted = [0].concat(arr, [total]).sort((a, b) => a - b);
let out = [];
for (i = 0; i < sorted.length - 1; i++) {
out.push(sorted[i+1] - sorted[i]);
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment