Skip to content

Instantly share code, notes, and snippets.

@a-tarasyuk
Created August 3, 2016 11:14
Show Gist options
  • Save a-tarasyuk/ca06639b53f74327b636687e357233ff to your computer and use it in GitHub Desktop.
Save a-tarasyuk/ca06639b53f74327b636687e357233ff to your computer and use it in GitHub Desktop.
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