Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Js Math.random not random

I can't generate a multidimensional array and fill it with random values, I tried to use for loops, forEach, and map with Math.random, but all arrays are identical.

function mat(a=5,b=5,max=2){
  
  let mt = new Array(a).fill(
    new Array(b).fill(0)
  )
  
  mt.map( (x,xk)=>{ 
    x.map( (y,yk)=>{ 
      mt[xk][yk] = Math.round(
        Math.random()*max
      )
    })
  })
  
  return mt
}
console.log(JSON.stringify(mat()))

output example

[[0,0,2,2,0],
 [0,0,2,2,0],
 [0,0,2,2,0],
 [0,0,2,2,0],
 [0,0,2,2,0]]

why?? does not make sense!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!