Ramdomize multiple arrays identically

T

Tuxedo

What is the best method for randomizing the items of two or more arrays
identically?

For example, the below function will sort the 'keys' array items in a
random order.

function ramdomize(){
return (Math.round(Math.random())-0.5);
}

var keys = new Array ("key1","key2","key3");
var values = new Array ("value1", "value2", "value3");

keys.sort(ramdomize);

How could the same random order be applied to the 'values' array?
 
E

Erwin Moller

Tuxedo said:
What is the best method for randomizing the items of two or more arrays
identically?

For example, the below function will sort the 'keys' array items in a
random order.

function ramdomize(){
return (Math.round(Math.random())-0.5);
}

var keys = new Array ("key1","key2","key3");
var values = new Array ("value1", "value2", "value3");

keys.sort(ramdomize);

How could the same random order be applied to the 'values' array?

Hi,

I don't think that can be accomplished in 1 go.
Probably simplest approach is:
1) Create an array MyIndexes, containing 0,1,2,3....
Put in there the number of elements your original array contains.
Doesn't matter which since they have the same amount of items.
2) randomize MyIndexes
3) read out MyIndexes, and rebuild the 2 arrays based on the found
indexes in MyIndexes.

Regards,
Erwin Moller
 
T

Tuxedo

Erwin Moller wrote:

[...]
I don't think that can be accomplished in 1 go.
Probably simplest approach is:
1) Create an array MyIndexes, containing 0,1,2,3....
Put in there the number of elements your original array contains.
Doesn't matter which since they have the same amount of items.
2) randomize MyIndexes
3) read out MyIndexes, and rebuild the 2 arrays based on the found
indexes in MyIndexes.

Thanks for explaining this procedure. So for example, by creating the two
arrays as well as the MyIndexes array for the randomization: ...

var keys = new Array ("key1","key2","key3");
var values = new Array ("value1", "value2", "value3");

var MyIndexes = new Array(0,1,2)
MyIndexes.sort(function() {return 0.5 - Math.random()})

.... how could I read out/rebuild the first two arrays based on the
MyIndexes order, and thereafter return both, in for example a
document.write() or alert() event?
 
T

Tuxedo

Randy Webb wrote:

[...]
Make it one array and sort it, keeping the keys/values together.

Do you mean separating each entry by an identifier of some kind and
thereafter divide them into two different arrays again?

If so, any small code examples would be greatly appreciated.
 
T

Tuxedo

I wrote:

[...]
... how could I read out/rebuild the first two arrays based on the
MyIndexes order, and thereafter return both, in for example a
document.write() or alert() event?

I think I understood it now, like for example:

var keys = new Array ("key1","key2","key3");
var values = new Array ("value1", "value2", "value3");

var MyIndexes = new Array(0,1,2)

MyIndexes.sort(function() {return 0.5 - Math.random()})

for(i = 0 ; i < MyIndexes.length ; i++)
document.write(keys[MyIndexes] + '<br>')

document.write('<hr>')

for(i = 0 ; i < MyIndexes.length ; i++)
document.write(values[MyIndexes] + '<br>')
 
T

Tuxedo

Randy Webb wrote:

[...]
var keysAndValues = new Array("key1:value1","key2:value2","key3:value3")

Then you randomize the array. Then you could just loop through it and
create the keys/values array from it.

Thanks, I will try this out - it sounds like a very good way to do it!
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Tue, 11 Dec 2007 13:57:37, Erwin Moller <Since_humans_read_this_I_am_s
(e-mail address removed)> posted:
That is a very bad way, in principle, of shuffling. <FAQENTRY> Don't do
that. </FAQENTRY>. Method sort is, IIRC, defined to require a stable
comparison; and with an unstable comparison the result is undefined. It
might never finish ... . Follow Knuth.
I don't think that can be accomplished in 1 go.
Probably simplest approach is:
1) Create an array MyIndexes, containing 0,1,2,3....
Put in there the number of elements your original array contains.
Doesn't matter which since they have the same amount of items.
2) randomize MyIndexes

No need for using 1 & 2 to get that. A slight simplification of the
standard shuffling algorithm gives a dealing algorithm.

function Shuffle(Q) { var J, K, T
for (J=Q.length-1 ; J>0 ; J--)
{ K = Random(J+1) ; T = Q[J] ; Q[J] = Q[K] ; Q[K] = T }
return Q }

function Deal(N) { var J, K, Q = new Array(N)
for (J=0 ; J<N ; J++)
{ K = Random(J+1) ; Q[J] = Q[K] ; Q[K] = J }
return Q }

Those could no doubt be modified to look more similar.

3) read out MyIndexes, and rebuild the 2 arrays based on the found
indexes in MyIndexes.


or 3a) Don't rebuild the arrays, but use MyIndexes to determine the
order in which they are read out.



Another approach - create a new array of objects (or of arrays) in which
each object (or array) contains the corresponding elements of the
original arrays. AOO[1] is {k:key1, v:value1} etc. Now randomize the
array of objects, then either read out into separate arrays or use as
they are.

Yet another approach - use the function Shuffle above, but modified to
take several array arguments (or an array of array arguments) and to
have a single main loop which exchanges elements in the same manner in
each array.


The OP should have noted what the FAQ says about shuffle.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
T

Tuxedo

Dr said:
In comp.lang.javascript message <[email protected]>
, Tue, 11 Dec 2007 13:57:37, Erwin Moller <Since_humans_read_this_I_am_s
(e-mail address removed)> posted:
That is a very bad way, in principle, of shuffling. <FAQENTRY> Don't do
that. </FAQENTRY>. Method sort is, IIRC, defined to require a stable
comparison; and with an unstable comparison the result is undefined. It
might never finish ... . Follow Knuth.
I don't think that can be accomplished in 1 go.
Probably simplest approach is:
1) Create an array MyIndexes, containing 0,1,2,3....
Put in there the number of elements your original array contains.
Doesn't matter which since they have the same amount of items.
2) randomize MyIndexes

No need for using 1 & 2 to get that. A slight simplification of the
standard shuffling algorithm gives a dealing algorithm.

function Shuffle(Q) { var J, K, T
for (J=Q.length-1 ; J>0 ; J--)
{ K = Random(J+1) ; T = Q[J] ; Q[J] = Q[K] ; Q[K] = T }
return Q }

function Deal(N) { var J, K, Q = new Array(N)
for (J=0 ; J<N ; J++)
{ K = Random(J+1) ; Q[J] = Q[K] ; Q[K] = J }
return Q }

Those could no doubt be modified to look more similar.

3) read out MyIndexes, and rebuild the 2 arrays based on the found
indexes in MyIndexes.


or 3a) Don't rebuild the arrays, but use MyIndexes to determine the
order in which they are read out.



Another approach - create a new array of objects (or of arrays) in which
each object (or array) contains the corresponding elements of the
original arrays. AOO[1] is {k:key1, v:value1} etc. Now randomize the
array of objects, then either read out into separate arrays or use as
they are.

Yet another approach - use the function Shuffle above, but modified to
take several array arguments (or an array of array arguments) and to
have a single main loop which exchanges elements in the same manner in
each array.


The OP should have noted what the FAQ says about shuffle.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

Unfortunately I didn't have time to read the FAQs on the topic of shuffling
beforehand. Also, the previously posted randomizing procedures appear to
work just fine, at least for the purpose, being a simple animation.
However, I will keep your comments and procedures in mind for next time.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Unfortunately I didn't have time to read the FAQs on the topic of shuffling
beforehand.

Until you show adequate sign of having read understood and remembered
FAQ 2.3, you will be ignored. The FAQ is not there just to save the
time of such as you; it is also to save the time of the experts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top