title: How can I make only one random space invader shoot?
body: <p>I have been working on making my own version of space invaders, I literally have everything working besides allowing my aliens to shoot, the problem is now I have a 2D array of aliens (15 by 3) that I want to have shoot a missile, each time it shoots I want it to come from a random alien, not all at once.</p>
  
  <p>Currently I have the missiles shooting at random intervals but all the aliens shoot at once making it near impossible to play the game.</p>
  
  <p>Here is a snippet of my code from where I have the aliens being drawn and also them shooting:</p>
  
  <pre><code>for (int j = 0; j &lt; 3; j++) { //Adds aliens on the screen (15 by 3)
    for (int i = 0; i &lt; 15; i++) {
      if (alien[i][j] != null) {
        alien[i][j].updateAlien();
        if (alien[i][j].edgeDetection() == true) { //If the edge is hit 
          for (int k = 0; k &lt; 3; k++) {
            for (int l = 0; l &lt; 15; l++) {
              if (alien[l][k] != null) {
                alien[l][k].deltaX = - alien[l][k].deltaX; //the aliens move the opposite direction
                alien[l][k].y = alien[l][k].y + 25; //the aliens move down a little
              }
            }
          }
        }
        for (int k = 0; k &lt; 3; k++) {
          for (int l = 0; l &lt; 15; l++) {
            if (alien[l][k] != null) {
              if (alienMissileCounter % 75 == 0) { //If the random int is divisible by 75 then the Aliens shoot a missile
                missileAlien.add(new MissileAlien(alien[l][k].x, alien[l][k].y, 10));
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I added the [l][k] bit on my missile thinking only one would shoot but that did the opposite.</p>
  
  <p>Here is also the for loop used to add the number of missiles:</p>
  
  <pre><code>    for (int m = missileAlien.size()-1; m &gt;= 0; m--) { //This adds one missile each time
    MissileAlien missilesalien = missileAlien.get(m); //This gets the number of missile i.e. 1
    missilesalien.updateMissile(); //This draws and moves the missile
  
    if (missilesalien.hitDefender(player) == true) { //If the defender is hit it loses a life
      missileAlien.remove(m);
      lives -= 1;
    }
  }
  </code></pre>
  
  <p>If you need me to paste any other code you think might help I will be more than happy to do so, it isn't much of a big deal as I have the rest of the game complete so if it isn't solvable then don't worry.</p>
  
  <p>UPDATE: After comments were made here is what my code looks like now: </p>
  
  <pre><code>    alienMissileCounter = int(random(0, 100));
  
  
  //Adds Aliens
  for (int j = 0; j &lt; 3; j++) { //Adds aliens on the screen (15 by 3)
    for (int i = 0; i &lt; 15; i++) {
      if (alien[i][j] != null) {
        alien[i][j].updateAlien();
        if (alien[i][j].edgeDetection() == true) { //If the edge is hit 
          for (int k = 0; k &lt; 3; k++) {
            for (int l = 0; l &lt; 15; l++) {
              if (alien[l][k] != null) {
                alien[l][k].deltaX = - alien[l][k].deltaX; //the aliens move the opposite direction
                alien[l][k].y = alien[l][k].y + 25; //the aliens move down a little
              }
            }
          }
        }
        for (int k = 0; k &lt; 3; k++) {
          for (int l = 0; l &lt; 15; l++) {
            if (alien[l][k] != null) {
              if (alienMissileCounter == 75) { //If the random int is divisible by 75 then the Aliens shoot a missile
                alienMissileCounter = 0;
                missileAlien.add(new MissileAlien(alien[l][k].x, alien[l][k].y, 10));
              }
            }
          }
        }
      }
    }
  }
  </code></pre>