Created
July 24, 2017 06:37
-
-
Save httnn/3c2d574934fd611375a0d386ba481136 to your computer and use it in GitHub Desktop.
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 slots = [50, 40, 200, 300, 400].sort((a, b) => a < b); | |
const slotSum = slots.reduce((sum, s) => sum + s, 0); | |
const random = () => { | |
const r = Math.random() * slotSum; | |
for (let i = 0, c = 0; i < slots.length; i++) { | |
c += slots[i]; | |
if (r < c) { | |
return i; | |
} | |
} | |
}; | |
let values = {}; | |
for (let i = 0; i < 10000; i++) { | |
const r = Math.floor(random() * 10) / 10; | |
values[r] = (values[r] || 0) + 1; | |
} | |
Object.keys(values).sort().map(key => { | |
return key + ': ' + values[key]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment