JSFiddle - React, Tailwind, and code Playground

by Pavel Kityan

JavaScript

function tryIt(n) {
    hat = [];
    var available = [];
    var count = 0;
    var n2 = n - 1;
    
    // random number
    var rnd = function () {
        return Math.round(Math.random() * available[n2]);
    }
    
    // gen list of available    
    i = 0;
    while (i < n) {
        available[i] = i;
        i++;
    }
    
    // fill the hats
    i = 0;
    while (i < n) {
        var z = rnd();
        if (i == available[z]) {
            console.log('#' + i + ' is lucky');
            count++;
        }
        available[z] = available[n2];
        n2--;
        if (i % (n / 10) === 0) { console.log('.........' + i) }
        i++;
    }
    

    console.log('===============================');
    console.log(count + ' out of ' + n);
    console.log('===============================');
}


tryIt(10000000);