0

What is the best way to shuffle a 2D array in javascript ?

I need my 2D array to be shuffled after being created.

will this be a good solution of doing this?

thx

  • Possible duplicate of [How can i shuffle an array in JavaScript?](http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript) – Hacketo Feb 22 '16 at 15:13
  • Possible duplicate of [How to randomize (shuffle) a JavaScript array?](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – Nina Scholz Feb 22 '16 at 15:20

2 Answers2

0

You can use lodash. It's javascript lib for array manipulation.

You can shuffle with lodash:

_.shuffle(yourArray);

https://lodash.com/docs#shuffle

Bato
  • 51
  • 1
  • 8
0

I have done that once for a Tile Map in a 2D Game.

for (let i = 0; i < mapSizeY; i++) {
    for (let j = 0; j < mapSizeX; j++) {
        var rand = Math.floor(Math.random() * 4) + 0;
        map[i][j] = rand;
    }
}

This generated a random Number between 0 and 3 for each tile

Gianmarco
  • 792
  • 2
  • 14
  • 38
Tibix
  • 400
  • 3
  • 12