3

I'm looking to get two different random items from the same array in JS. There are relating questions on Stack Overflow but I fail to understand how the Fisher Yates Shuffle works. I need to search the whole array to retrieve these items, but the array is small in size.

Currently I have a while loop but this doesn't seem to be the most efficient way of acheiving:

    var honeyPots = ["Fname", "EmailAddress", "Lname", "Telephone", "Address1", "Address2", "Surname", "Title"]; //Fake field names to dupe the bots!
    var honeyPot = honeyPots[Math.floor(Math.random()*honeyPots.length)]; //Get a random field name from the array
    var honeyPot2 = honeyPots[Math.floor(Math.random()*honeyPots.length)]; //Get a random field name from the array
    while (honeyPot == honeyPot2)
      {
        var honeyPot2 = honeyPots[Math.floor(Math.random()*honeyPots.length)];
      }

3 Answers 3

6

Just shuffle the array and get first two items:

var honeyPots = ["Fname", "EmailAddress", "Lname", "Telephone", "Address1", "Address2", "Surname", "Title"];

var results = honeyPots
    .sort(function() { return .5 - Math.random() }) // Shuffle array
    .slice(0, 2); // Get first 2 items

var honeyPot = results[0];
var honeyPot2 = results[1];
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry to be pedantic, is that sort function a similar concept to that which the Fisher Yates Shuffle defines?
No, it's another approach.
There is an excellent article with visualizations of array sort methods here: bost.ocks.org/mike/shuffle Assuming I've set it up correctly, the final method on that page performs better than the one in this answer: jsben.ch/wotnS. Not a consideration in most cases, probably, and the one here succinct!
0

You can do it like,

var arr = [1,2,3,4,4,5,8];
var randomValue = [];
for(i=arr.length; i>=0; i--) {
  var randomNum = Math.floor(Math.random() * i);
  randomValue.push(arr[randomNum]);
  if(i==arr.length-1)break;
}
console.log(randomValue);

Hope it helps.

Comments

0

Based on @alexey-prokhorov's answer but using a different method for shuffling the array, you could do something like:

var getRandosFromArray = function(array, numRandos){
  var shuffled = shuffle(array)
  var randos = shuffled.slice(0, numRandos)
  return randos
}

// https://bost.ocks.org/mike/shuffle/
var shuffle = function(array) {
  var m = array.length, t, i;

  // While there remain elements to shuffle…
  while (m) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }

  return array;
}

That way you have a generic function to which you can just pass an array and the number of random items (which are returned in an array) you want back from it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.