Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.