lundi 6 janvier 2020

Random array between given range of integers, containing at least one instance of each integer in the range

I'm trying to generate an array that contains a random set of integers between a range that will contain at least one instance of each possible integer. I can generate an array with random integers between a range, but cannot figure out how to generate one that contains at least one instance of each integer in said range. I'm trying to generate an array that contains 84 keys with a range between 0 and 40 (including 0 and 40). The array should contain at least one instance of each possible integer in said range. This is the code that I have right now:

<script>
    ints = [];

    function sortInt(a, b) {
        return a - b;
    }

    for (i=0; i<84; i++) {
        ints.push(Math.round(Math.random() * 40));
    }

    ints.sort(sortInt);

    for (s=0; s<ints.length; s++) {
        document.write(ints[s] + '<br />');
    }
</script>



Aucun commentaire:

Enregistrer un commentaire