0

I am trying to write code to select n random elements from an array. For example:

const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

const randomSelection = (n) => {
    if (n > originalArray.length) {
        return originalArray;
    } else {
        //Some Code to randomly select n elements from orignialArray
    }
}

const newArray = randomSelection(2); //Random selection of 2 elements from the above array

What would be the code to achieve this? I searched for case examples but wondered if there is a simpler and more straightforward way to do this.

Any advice? Thanks a lot in advance!

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
J.Ko
  • 6,561
  • 3
  • 12
  • 28
  • Does this answer your question? [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) – michaeldel May 08 '20 at 03:54

4 Answers4

3

This code produces n random elements from the array, and avoids duplicates:

const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

const randomSelection = (n) => {
  let newArr = [];
  if (n >= originalArray.length) {
    return originalArray;
  }
  for (let i = 0; i < n; i++) {
    let newElem = originalArray[Math.floor(Math.random() * originalArray.length)];
    while (newArr.includes(newElem)) {
      newElem = originalArray[Math.floor(Math.random() * originalArray.length)];
    }
    newArr.push(newElem);
  }
  return newArr;
}

console.log(randomSelection(2));
console.log(randomSelection(5));
.as-console-wrapper { max-height: 100% !important; top: auto; }

If you don't care about duplicates, remove the while:

const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

const randomSelection = (n) => {
  let newArr = [];
  if (n >= originalArray.length) {
    return originalArray;
  }
  for (let i = 0; i < n; i++) {
    let newElem = originalArray[Math.floor(Math.random() * originalArray.length)];
    newArr.push(newElem);
  }
  return newArr;
}

console.log(randomSelection(2));
console.log(randomSelection(5));
.as-console-wrapper { max-height: 100% !important; top: auto; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

Try this:

const originalArray=["a","b","c","d","e","f","g","h","i"];


function RandomElements(n){
  const newArray = []

    if (n>originalArray.length) {
        return originalArray;
    }
    else {
      for(let i = 0; i < n; i++){
    newArray.push(originalArray[Math.floor(Math.random() * originalArray.length)])
  }
  return newArray;
    }

}

console.log(RandomElements(2))
chanaka
  • 121
  • 8
1

You could do something like

let randArr = [];
while(let i = 0; i < n; i++)
     let chosen = Math.floor(Math.random() *  originalarray.length);

     randArr[i] = originalarray[chosen];
}
return randArr;

So then you could retrieve more than one random value from the array.

s6xy
  • 398
  • 1
  • 11
1
function randArr(n) {
    let ta = arr, i=0;
    while(i<n) {
        let ri = Math.floor(Math.random()*ta.length);
        ta[i] = [ta[ri], ta[ri]=ta[i]][0];
        i++;    
    }
    ta.splice(i, ta.length);
    return ta;
}

The array elements wont be repeated when random is selected.

Jerin
  • 688
  • 1
  • 9
  • 21