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

214
Views
Trying to create a matrix with random numbers in JavaScript produces a matrix where all the rows are the same

When I try to create a matrix with random numbers, the matrix rows all appear the same. How do I fix this? The code for the problem is:

var sudokuRowMatrix = [];
var numbers = [];
var randomizedArray = [];
var sudokuColumnArray = [];
for(i=0;i<=8;i++){
    numbers[i]=i+1;
    sudokuRowMatrix[i] = [];
    sudokuColumnArray[i] = [];
}

function RandomizeArray(array){
    for(arrayIndex=0;arrayIndex<=8;arrayIndex++){
        randomArrayIndex = Math.floor(Math.random()*9);
        randomArrayIndex2 = Math.floor(Math.random()*9);
        placeHolder = array[randomArrayIndex];
        array[randomArrayIndex]=array[randomArrayIndex2];
        array[randomArrayIndex2] = placeHolder;
    }
    return array;
}

for(rowNumber=0;rowNumber<=8;rowNumber++){
    sudokuRowMatrix[rowNumber] = RandomizeArray(numbers);
}

All the rows of the matrix are the same

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you call RandomizeArray(numbers) you pass the numbers array as a reference. This means, that you always shuffle the same array and set it as a row in your Sudoku matrix and therefore shuffle all of your rows since they all hold the same reference.

An easy way to fix this, is to destructure your array before passing it to your method: sudokuRowMatrix[rowNumber] = RandomzieArray([...numbers]);

You can find my source here.

about 4 years ago · Juan Pablo Isaza Report
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!