var links = [ { url: "https://web.archive.org/web/20091001001238/http://www.msn.com/", weight: 1 }, // 5% { url: "https://web.archive.org/web/20090331121154/http://www.msn.com/", weight: 2 }, // 10% { url: "https://web.archive.org/web/20090831230448/http://www.msn.com/", weight: 3 }, // 15% { url: "https://web.archive.org/web/20090301053317/http://www.msn.com/", weight: 4 }, // 20% { url: "https://web.archive.org/web/20090731104906/http://www.msn.com/", weight: 10 } // 50% ]; function weightedRandom(arr) { var weightSum = arr.map(l => l.weight).reduce((a, b) => a + b, 0); var rand = Math.random() * weightSum; var accumulated = 0; for (var i = 0; i < arr.length; i++) { if (rand < accumulated + arr[i].weight) { return arr[i]; } accumulated += arr[i].weight; } } function openSite() { var link = weightedRandom(links); // uncomment this and call openSite function: //var win = window.open(link.url, '_self'); //win.focus(); } function experiment() { var testsCount = 10000; var counts = {}; for (var i = 0; i < links.length; i++) { counts[links[i].url] = 0; } for (var i = 0; i < testsCount; i++) { var link = weightedRandom(links); counts[link.url]++; } console.log(counts); } experiment();