JRS: In article <11*********************@g47g2000cwa.googlegroups. com>,
dated Fri, 8 Jul 2005 10:29:50, seen in news:comp.lang.javascript,
bg***@yahoo.com posted :
I have an array as follows -
var MultiArray = new Array(2);
MultiArray [0] = new Array(num);
MultiArray [1] = new Array(num);
I've tried randomizing this array using the following but its obviously
wrong -
for (i=0;i<noOfVerbs;i++) {
MultiArray[0]=MultiArray[0].sort(randomSort);
MultiArray[1]=MultiArray[1].sort(randomSort);
}
function randomSort(w1,w2)
{
return Math.floor(Math.random()*3)-1;
}
I want to randomize the array, but if MultiArray[0][1] becomes
MultiArray[0][13], then I want MultiArray[1][1] to become
MultiArray[1][13], and so on.
How can I do this?
Rather a confusing description.
You should not randomise by random sorting; there are better, easy
methods. FAQ 4.22 has a link on shuffling.
The .sort routine is written in the expectation that the result of
randomSort(w1,w2) depends on, and only on, w1,w2, reproducibly. FAQ.
For your purpose, if I understand it, which I doubt, you should shuffle
the outer 1D array, in which each entry is itself an array. That means
either redesigning your algorithm to use the indexes in reverse order,
or transposing, shuffling, and re-transposing.
Another approach would be to deal - FAQ 4.22 has a link on dealing -
integers into an array of size num, then to copy your 2D array with the
position of each element in the new sub-arrays given by lookup in the
once-dealt array.
You could write your own random function, initialisable by yourself, so
that after each initialisation it would generate the same sequence, thus
being able to random-sort each sub-array identically. But not a good
idea. FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.