-1

I am trying to fill a 2D Array with random 1 and zeros the code that I wrote is

new Array(16).fill(null).map(()=>new Array(16).fill(Math.round(Math.random())));

The problem with this code that I end up with something like

0: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
2: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
3: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
5: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
6: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
7: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
8: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
9: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
10: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
11: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
12: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
13: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
14: (16) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
15: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

I want something like

0: (16) [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1]
1: (16) [1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1]
2: (16) [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
3: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
4: (16) [0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1]
5: (16) [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
6: (16) [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
7: (16) [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
8: (16) [1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1]
9: (16) [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
10: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
11: (16) [0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
12: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
13: (16) [0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
14: (16) [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
15: (16) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

I want a random number in each of them and not the same number for all of the rows.

JS Lover
  • 622
  • 5
  • 16

1 Answers1

3

From Array.prototype.fill():

The fill() method fills all the elements of an array from a start index (default zero) to an end index (default array length) with a static value.

So, per inner array, the same Math.round(Math.random()) value is filled

In the below example, Math.random() is evaluated once and that static value is repeated 5 times:

console.log(Array(5).fill(Math.random()))

Instead, you could use nested Array.from() like this:

const createMatrix = length => 
  Array.from({ length }, _ => 
      Array.from({ length }, _ => Math.round(Math.random())))

console.log(JSON.stringify(createMatrix(3)))
console.log(JSON.stringify(createMatrix(6)))

Here's a detailed version:

function createMatrix(n) {
    // creates an arary with length = n
    const arrayOfLengthN = Array.from({ length: n });
    
    // map the array and create a 2D array
    return arrayOfLengthN.map(a => Array.from({ length: n })
                                 .map(b => Math.round(Math.random())))

}
console.log(JSON.stringify(createMatrix(3)))
console.log(JSON.stringify(createMatrix(6)))
adiga
  • 34,372
  • 9
  • 61
  • 83
  • Can you please add comments to your code, I don't understand why do you pass an object to `Array.from({ length })` – JS Lover Jul 24 '19 at 16:33
  • I really like your code and it's short and to the point but I don't understand it. @adiga – JS Lover Jul 24 '19 at 16:36
  • 1
    @JSLover I have added another filddle with broken down steps. Please go through the documentation of `Array.from()`. The first parameter should be an array like obejct OR an object with a length property. The will create an array with `n` undefined values. The second parameter is the `map` callback to run on *each of the array elements*. Here, the mapping function you return another `Array.from()` because you want to create the 2D array/ This bit is similar ot how you are doing with `Array.fill()` – adiga Jul 24 '19 at 16:43
  • 1
    @JSLover `Array.from({ length: n }, b => Math.round(Math.random()))` is same as `Array.from({ length: n }).map(b => Math.round(Math.random()))` – adiga Jul 24 '19 at 16:45