title: Java Math.random with arrays
body: <p>I am trying to populate my 2d array that is 5 by 5 with a char such as A in random coordinates in the 2d array. When I use my nested for loop i wanted to make the coordinates of the 2d array where my char will be to be random. So lets say I asked for 40 percent of A's, in a 5 by 5 I should get 10 A's but I get 8. When i run it, it doesn't show the percentage of A's i wanted sometimes. it would only print out like 6. Is this because when the row and col in the if statement are randomized, so is the row and col in the for loop? Is this why the char sometimes populates less then asked for because the for loop stops if the number randomizes the length of the 2d array which is 5? </p>
  
  <p>Also when it does print out 10 char, sometimes they go over the 5 by 5. an example would be 2 As in the line and 7 in the second and 1 in the 3rd. Why is that?</p>
  
  <pre><code>public static void grid(char[][] arr, int percentofchar)
  {
  
    double charx = 0.0;
  
    double totalpercentchar = 0;
    totalpercentchar = (double) percentofchar / 100;
  
    cellx = totalpercentchar * (arr.length * arr.length);
  
    for (int row = 0; row &lt; arr.length; row++)
    {
      for (int col = 0; col &lt; arr[row].length; col++)
      {
        if (charx &gt; 0)
        {
          row = (int) (Math.random() * arr.length);
          col = (int) (Math.random() * arr.length);
          charx--;
          arr[row][col] = 'A';
          System.out.print(arr[row][col] + "  ");
        }
      }
      System.out.println();
    }
  }
  </code></pre>