Skip to content

Instantly share code, notes, and snippets.

@jjgonecrypto
Created December 4, 2018 19:48
Show Gist options
  • Save jjgonecrypto/0a5de7bf740368323f9ddd5048b0f212 to your computer and use it in GitHub Desktop.
Save jjgonecrypto/0a5de7bf740368323f9ddd5048b0f212 to your computer and use it in GitHub Desktop.
Function to split up some given number into a fixed number of parts.
const random = require('random-js')();
// Splits up number into times random parts
const splitter = (number, times) => {
const ran = random.real(0, number);
if (times < 2) {
return [number];
}
const halved = times / 2;
if (times % 2 === 0) {
return splitter(ran, halved).concat(splitter(number - ran, halved));
} else {
return splitter(ran, Math.ceil(halved)).concat(
splitter(number - ran, times - Math.ceil(halved))
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment