JSFiddle - React, Tailwind, and code Playground
CSS
body {
background: white;
}
JavaScript
// Variables
var sum, rand, result, i;
// Data
var teams = [
{ "selections": 0, "team": "New England Patriots", "rank": 100 },
{ "selections": 0, "team": "Pittsburgh Steelers", "rank": 93.75 },
{ "selections": 0, "team": "Jacksonville Jaguars", "rank": 87.5 },
{ "selections": 0, "team": "Kansas City Chiefs", "rank": 81.25 },
{ "selections": 0, "team": "Tennessee Titans", "rank": 75 },
{ "selections": 0, "team": "Buffalo Bills", "rank": 68.75 },
{ "selections": 0, "team": "Baltimore Ravens", "rank": 62.5 },
{ "selections": 0, "team": "Los Angeles Chargers", "rank": 56.25 },
{ "selections": 0, "team": "Cincinnati Bengals", "rank": 50 },
{ "selections": 0, "team": "Oakland Raiders", "rank": 43.75 },
{ "selections": 0, "team": "Miami Dolphins", "rank": 37.5 },
{ "selections": 0, "team": "Denver Broncos", "rank": 31.25 },
{ "selections": 0, "team": "New York Jets", "rank": 25 },
{ "selections": 0, "team": "Indianapolis Colts", "rank": 18.75 },
{ "selections": 0, "team": "Houston Texans", "rank": 12.5 },
{ "selections": 0, "team": "Cleveland Browns", "rank": 6.25 }
];
// Weighted Randomization
function randomize(array, key) {
sum = array.reduce(function(accumulator, item) {
return accumulator + item[key];
}, 0);
rand = ~~(Math.random() * sum);
for (var i = 0; i < array.length; i++) {
rand -= array[i][key];
if (rand <= 0) break;
}
return array[i];
}
// Results
for (i = 0; i < 5000; i++) {
result = randomize(teams, "rank");
result.selections++;
}
// Print
for (i in teams) {
document.body.innerHTML += [
teams[i].selections, " - ", teams[i].team, "<br>"
].join("");
}