Jump to content

randomize array (2D)


zero_point

Recommended Posts

Hi all,

I have a little problem, I know how to randomize an array like this ...

 

function suffleArray(array){

for (var i = array.length - 1; i > 1; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array; array = array[j]; array[j] = temp; }

}

... but when I use this function in 2D array like this ...

 

var column = new Array(5);var row = [1,2,3,4,5];for(var i=0;i<5;i++){ column = suffleArray(row);}

for(var i=0;i<5;i++){ for(var j=0;j<5;j++){ document.write(column[j]); }document.write("<br>");}

---------------------------------------------

output ... for example

2 0 1 4 3

2 0 1 4 3

2 0 1 4 3

2 0 1 4 3

2 0 1 4 3

 

... I have all rows same and I do not know why. Can you help me? Thank you.

Link to comment
Share on other sites

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>shuffle</title><script>var arr1 = [1,2,3,4,5];var arr2 = [];window.onload = init;function init() {document.getElementById('btn1').onclick = process;document.getElementById('btn2').onclick = build2d;}function process(){arr2 = shufflearr(arr1);displayarr();}function build2d(){var row = [];var r = [1,2,3,4,5];var rows = 10;for(var i=0 ; i<rows ; i++){    row[i] = shufflearr(r);}str ='';for(var i=0 ; i<rows ; i++){   for(var j=0 ; j<r.length ; j++){      str += row[i][j] +' ';   }   str += '<br/>';}document.getElementById('out1').innerHTML = str;}function shufflearr(array) {var temp = [];var out = [];var len = array.length;var i;for(i=0 ; i<len ; i++){temp[i]=array[i];}i=0;var j;while (i<len){j = Math.floor(Math.random() * len);if(temp[j]!=null){out[i]=temp[j];temp[j]=null;i++;}}return out;}function displayarr(){var str = 'arr1.length='+ arr1.length +'<br/>arr2.length='+ arr2.length +'<br/>';for(var i=0,len=arr1.length ; i<len ; i++){str += 'i='+ i +' arr1['+i+']='+ arr1[i] +' arr2['+i+']='+ arr2[i] +'<br/>';}document.getElementById('out1').innerHTML = str;}</script></head><body><input type="button" id="btn1" value="Shuffle"/><input type="button" id="btn2" value="Build 2D"/><div id="out1"></div></body></html>
Edited by davej
  • Like 1
Link to comment
Share on other sites

They're all pointing to the same array. What you need to do is make a copy of that array before shuffling it.

I try to make a copy as you said .... like this ...

 

for(var i=0;i<5;i++){ newA = suffleArray(row); column = newA;}

 

... or ... this way ...

 

for(var i=0;i<5;i++){ column = row; column = suffleArray(column);}

 

... same problem

... probably it was not a good idea, but I do not have other ... can you send me a code of that little change? Thank you.

Edited by zero_point
Link to comment
Share on other sites

//shuffles the array recursivelyfunction shuffleArray_r(list){    //recursive breaker    if (list.length == 0)         return list;    //copy to new array so that the original list isn't affected    list = [].concat(list);     var newlist = list.splice(Math.floor(Math.random()*(list.length)),1);    return newlist.concat(shuffleArray_r(list));}var myoldlist = [1,2,3,4,5];var mynewlist = shuffleArray_r(myoldlist);
Link to comment
Share on other sites

I think it is strange that I seem to need to use a rather odd scheme just to auto-size a 2d array...

function build2db(){var a = [];var rows = 24;var cols = 7;var max = 9;for(var i=0 ; i<rows ; i++){  row = new Array(cols);  for(var j=0 ; j<cols ; j++){    row[j] = 1000+j+10*i;  // Math.floor(Math.random() * (max+1));  }  a[i] = row;}str ='';for(var i=0 ; i<rows ; i++){  for(var j=0 ; j<cols ; j++){    str += a[i][j] +' ';  }  str += '<br/>';}document.getElementById('out1').innerHTML = str;}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...