user3652674
user3652674

Reputation: 27

Randomize array, pick up 2 random objects, and populate a new array?

I have array with multiple objects, for instance,

var arr = ["a", "b", "c", "d"];

What I want is to pick up 2 random objects from that array, for instance "a" and "c", and push the 2 objects into another array, for instance "arrCopy". Another thing is, I want to have a copy of the elements in the array "arrCopy", which are also shuffled. So, that array should look like:

arrCopy = ["c", "a", "a", "c"];

Last thing, how can I compare them if their content is the same? For instance:

arrCopy[1] == arrCopy[2];

Upvotes: 0

Views: 114

Answers (4)

juvian
juvian

Reputation: 16068

This should do the trick:

function shuffle(array) {
  var currentIndex = array.length
    , temporaryValue
    , randomIndex
    ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var arr = [{a:{count:3}}, {b:{count:2}}, {c:{count:3}}, {d:{count:4}}];

var num1,num2;
var shuffled_arr=shuffle(arr);
item1=arr[0];
item2=arr[1];

var arrCopy=shuffle([item1,item2,item1,item2]);

for(var i=0;i<arrCopy.length;i++){
    for(var g=i+1;g<arrCopy.length;g++){
        if(JSON.stringify(arrCopy[i])==JSON.stringify(arrCopy[g])){
            console.log('item in index ' + i +' is equal to item in index '+ g)
            console.log(arrCopy[i], arrCopy[g])
        }
    }    
}

Upvotes: 0

David
David

Reputation: 251

For your first question:

var index1 = parseInt(Math.random() * arr.length, 10);
var index2 = index1;
while(index1 == index2) {
  index2 = parseInt(Math.random() * arr.length, 10)
}

var arrCopy = [arr[index1], arr[index2]]

Can you clarify the second part of your question, it doesn't sound like 'Another thing is, I want to have a copy of the elements in the array "arrCopy"' matches up with your example since your example has 4 elements instead of the 2 you picked from the array.

Edit:

For your second question replace:

var arrCopy = [arr[index1], arr[index2]]

with:

var arrCopy = [arr[index1], arr[index2], arr[index1], arr[index2]]

For your third Question:

arrCopy1 === arrCopy2 would be the shallow comparison (are they the same address in memory)

var areEqual = true;

for (var i = 0; i < arrCopy1.length; ++i) {
    if (arrCopy1 [i] !== arrCopy2[i]) {
        areEqual = false;
        break;
    }
}

then check areEual for deep equality (whether array content are the same).

Upvotes: 1

Avinash Babu
Avinash Babu

Reputation: 6252

Found a simplest way from here..Sort helps to do this in a shorter way

[1,2,3,4,5,6].sort(function() {
  return .5 - Math.random();
});

Upvotes: 0

Fyre
Fyre

Reputation: 1180

Use any randomization algorithm(For eg: fisher yates algorithm)..http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Randomize the objects in the array and

Pick any two elements like

var firstElement = arr[Math.floor(Math.random()*arr.length)]
var secElement = arr[Math.floor(Math.random()*arr.length)]

Upvotes: 0

Related Questions