Created
August 3, 2016 11:14
-
-
Save a-tarasyuk/ca06639b53f74327b636687e357233ff 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
| function rand(min, max) { | |
| return Math.floor(Math.random() * max) + min | |
| } | |
| function getRandomValue(list, weight) { | |
| var totalWeight = weight.reduce((p, c) => p + c, 0); | |
| var randomRatio = rand(0, totalWeight); | |
| for (var i = 0; i < list.length; i++) { | |
| if (randomRatio < weight[i]) { | |
| return list[i]; | |
| } | |
| randomRatio = randomRatio - weight[i]; | |
| } | |
| return false; | |
| } | |
| setInterval(() => { | |
| console.log(getRandomValue(['js', 'php', 'ruby'], [90, 5, 5])); | |
| }, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment