I try to fill the array s with 9999 different sets of values m[i][random], here is the code:
let m = [[22,0],[53,0],[64,0],[45,0],[34,0]];
let l = m.length;
let s = [];
for (let j = 0; j < 9999; j++)
{
for(let i = 0; i < m.length; i++)
{
let x = Math.floor(Math.random()*l);
m[i][1] = x;
}
s.push(m);
}
But i get the same values:
console.log(s)
[ [ [ 22, 0 ], [ 53, 2 ], [ 64, 0 ], [ 45, 4 ], [ 34, 1 ] ],
[ [ 22, 0 ], [ 53, 2 ], [ 64, 0 ], [ 45, 4 ], [ 34, 1 ] ],
[ [ 22, 0 ], [ 53, 2 ], [ 64, 0 ], [ 45, 4 ], [ 34, 1 ] ],
[ [ 22, 0 ], [ 53, 2 ], [ 64, 0 ], [ 45, 4 ], [ 34, 1 ] ],
[ [ 22, 0 ], [ 53, 2 ], [ 64, 0 ], [ 45, 4 ], [ 34, 1 ] ],
[ [ 22, 0 ], [ 53, 2 ], [ 64, 0 ], [ 45, 4 ], [ 34, 1 ] ],
[ [ 22, 0 ], [ 53, 2 ], [ 64, 0 ], [ 45, 4 ], [ 34, 1 ] ], ...]
What am I doing wrong? How to fix it?
mon every iteration of the outer loop.sfor the first several iterations of the outer loop. In this case, that would have ruled out one possible problem (the same random values being produced each time) and helped identify the actual problem (the random values from the previous iterations get replaced with the ones from the latest iteration). That would get you closer to the root cause (scontains repeated references to the same arraym, so new updates tomchange your previous additions tos).