1

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>
user1885099
  • 130
  • 2
  • 6
  • 28
  • As I can see, whether your final answer should be sorted as well? – Nikhil Goyal Jan 06 '20 at 21:12
  • First create an instance of each required integer, then append some more random elements up to whatever size you want. – nurdyguy Jan 06 '20 at 21:12
  • @NikhilGoyal The final answer should be sorted. – user1885099 Jan 06 '20 at 21:15
  • @nurdyguy I've tried generating an array that contains the desired integers and then randomly appending them to the final array. While this does improve on the probability of the final array to contain at least one instance, it does not guarantee it. – user1885099 Jan 06 '20 at 21:17
  • @user1885099 If you created it containing the desired integers the it is guaranteed it will contain the desired integers, unless you did it wrong. If you make an array with `[1, 2, 3]` then it _will_ have each of `1`, `2`, and `3` in it. – nurdyguy Jan 06 '20 at 21:24

4 Answers4

0

Please try the below snippet

function generateNumbers(number, startRange, endRange) {
    if (startRange > endRange) return generateNumbers(number, endRange, startRange);
    
    const arr = [];
    for (let i = startRange; i <= endRange; ++i) arr.push(i);
    
    while(number > arr.length) {
        const newNumber = Math.round(Math.random() * (endRange - startRange));
        arr.push(newNumber);
    }
    
    arr.sort((a, b) => a - b);
    
    return arr;
}

console.log(generateNumbers(84, 0, 40));
Nikhil Goyal
  • 1,945
  • 1
  • 9
  • 17
0

I think if it needs all the integers between a range then you can start with generating between the range. Then all the other numbers can be random. Finally just need to use sort() on the array.

Like this:

const generate = (till, count) => {
  const array = Array.from(new Array(till + 1).keys());

  for (let i = till; i < count; i++) {
     array.push(Math.round(Math.random() * till));
  }
    
  return array.sort((a,b) => a - b);
}

const result = generate(40, 84);
console.log(result);

I hope that helps!

norbitrial
  • 14,716
  • 7
  • 32
  • 59
0

You can create random index to insert a number from the desired range:

if (i == randomIndex)
    ints.push(startRange);
else
    ints.push(Math.floor(Math.random() * endRange) + startRange);

An example:

ints = [];

let startRange = 1, endRange = 84;
let randomIndex = Math.floor(Math.random() * endRange) + startRange;

for (i=0; i < 84; i++) {
    if (i == randomIndex)
        ints.push(startRange);
    else
        ints.push(Math.floor(Math.random() * endRange) + startRange);
}

ints.sort((a,b) => a - b);
console.log(ints)
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

I'd use randojs.com for this. Grab a shuffled array from 0 to 40, then add 43 more random numbers between 0 and 40 to the array and sort:

var ints = randoSequence(40);
for(var i = 0; i < 43; i++) ints.push(rando(40));
console.log( ints.sort((a, b) => {return a - b;}) );
<script src="https://randojs.com/1.0.0.js"></script>
Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15