Created
December 4, 2018 19:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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