Filling in random positions in a huge 2D array
<p>Is a there a neat algorithm that I can use to fill in random positions in a huge 2D <em>n</em> x <em>n</em> array with <em>m</em> number of integers without filling in an occupied position? Where <img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=n%20%5Cgeq%20100" alt="nformula">, and <img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=m%20%5Cleq%20%5Cfrac%7Bn%5E%7B2%7D%7D%7B90%7D" alt="mformula"></p>

<p>Kind of like this pseudo code:</p>

<pre><code>int n;
int m;

void init(int new_n, int new_m) {
    n = new_n;
    m = new_m;
}
void create_grid() {
    int grid[n][n];

    int x, y;
    for(x = 1; x &lt;= n; x ++) {
        for(y = 1; y &lt;= n; y ++) {
            grid[x][y] = 0;
        }
    }

    populate_grid(grid);
}

void populate_grid(int grid[][]) {
    int i = 1;
    int x, y;

    while(i &lt;= m) {
        x = get_pos();
        y = get_pos();

        if(grid[x][y] == 0) {
            grid[x][y] = i;
            i ++;
        }
    }
}

int get_pos() {
    return random() % n + 1;
}
</code></pre>

<p>... but more efficient for bigger n's and m's. Specially if m is bigger and more positions are being occupied, it would take longer to generate a random position that isn't occupied.</p>