Randomize an array in javascript

This is the sample post of randomize array concept in javascript; In the below example i will show you to randomize an array using its position in java script.

var _arrayOne=[1,2,3,4,5,6,7];
var _arrayTwo=[27,26,24,23,29,28,25];
var _randArrayOne=[];
var _randArrayTwo=[];

function random(){

document.write("Before Randomize");
document.write('\n');
document.write(_arrayOne);
document.write('\n');
document.write(_arrayTwo);
document.write('\n');

for(var i=0; i < 7; i++)
{
var _random = Math.round(Math.random()*(_arrayOne.length-1));
_randArrayOne.push(_arrayOne[_random]);
_randArrayTwo.push(_arrayTwo[_random]);
_arrayOne.splice(_random,1);
_arrayTwo.splice(_random,1);
}

document.write("After Randomize");
document.write('\n');
document.write(_randArrayOne);
document.write('\n');
document.write(_randArrayTwo);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *