mercredi 5 juin 2019

Randomly filling a 2D matrix

I'd like to set each element of a 12x12 array to zero or one, chosen randomly. I attempted to use Math.round(Math.random()) to make this selection, and this seems to be causing the problem (when I set the values to something else, the program works fine)

var board = [];

for (var i=0; i<12; i++)
{
    board.push([]);
    for (var j=0; j<12; j++)
    {
        board[i].push(Math.round(Math.random()));
    }
}
console.log(board);

If I change the argument in .push() the program works as expected. As it is however, there is no output at all.




Aucun commentaire:

Enregistrer un commentaire