title: Maximizing efficiency of this while loop
body: <p>I have a while loop that is supposed to randomly plant numberOfTruth true values in a 2 dimensional. I am only to change them if they are not true already, and only do so for numberOfTruths times: </p>
  
  <pre><code>function truthfulArray(maxY, maxX, numberOfTruths, array) {
    var x;
    var y;
    var counter = 0;
  
    while (counter &lt; numberOfTruths) {
      x = generateRandomNumber(maxX);
      y = generateRandomNumber(maxY);
  
      if (!array[x][y]) {
        array[x][y] = true;
        counter++;
      }
    }
  
    return array;
  }
  </code></pre>
  
  <p>I could see the issue with its optimization in relation to the size of 2 dimensional array and as the numberOfTruths approaches the product of maxY and maxX it would waste alot of resources to complete the task. I was wondering what tweaks I can make to the function to make it more efficient. Thanks in advance!</p>
  
  <p>*** <code>generateRandomNumber(max)</code> is a simple function that returns a random number from 0 to the max value entered.</p>