1

How to get random items from an array with no repeat?

I have an array of elements like

var a = ["Mango", "Orange", "Banana", "Apple", "Grapes", "Berry", "Peach"]

I want to get 3 random items from array a

var random = ["Banana", "Berry", "Peach"]
Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59

5 Answers5

2

var a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"];

function searchRandom(count, arr){
  let answer = [], counter = 0;
 
  while(counter < count){
    let rand = arr[Math.floor(Math.random() * arr.length)];
    if(!answer.some(an => an === rand)){
      answer.push(rand);
      counter++;
    }
  }
  
  return answer;
}

console.log(searchRandom(3,a))

Making it flexible to support any count you want and ensured uniqueness

Isaac
  • 12,042
  • 16
  • 52
  • 116
1

var a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"]
var res = a.sort(function() {
  return 0.5 - Math.random();
});
console.log(res.slice(a,3))
vicky patel
  • 699
  • 2
  • 8
  • 14
  • Careful, `.sort` sorts the array _in place_, modifying the original one. You should make a copy and sort that one if you don't want any side effect. – Alvaro Castro May 02 '21 at 00:19
0

Try this:-

var a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"]
b = _.shuffle(a).slice(0,5);
console.log(b);
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
Mr. Roshan
  • 1,777
  • 13
  • 33
0

This should work without any duplicates:

const a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"]
const b = a.slice()
const newArr = [];

for(let i= 0; i<3; i++){
 let arr = b[Math.floor(Math.random()*b.length)];
  
 let index = b.indexOf(arr);
  
  b.splice(index, 1 );
  
  newArr.push(arr)
  
}

console.log(newArr)
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
0

i would like to improve the Willem van der Veen's answer because his code might return undefined. It stems from the fact that the arrayClone takes out the random item but random number still in the range of the previous index length. Hence, the solution is we have to decrease the arrayClone's length too:

    const array = [0, 1 ,2 ,3 ,4, 5, 6, 7, 8 , 9]

    const arrayClone = array.slice()

    let arrayLength = array.length

    let newArray = []

    for (  let i = 0; i < 5; i++) {
        let arr = arrayClone[Math.floor(Math.random() * arrayLength)]

        arrayLength--

        let index = arrayClone.indexOf(arr)

        arrayClone.splice(index, 1)

        newArray.push(arr)
     }


      console.log(newArray)
Flair
  • 2,609
  • 1
  • 29
  • 41
  • Good answer, and we can improve little bit like this; const array = [0, 1 ,2 ,3 ,4, 5, 6, 7, 8 , 9] const arrayClone = array.slice(); let arrayLength = array.length; let newArray = []; for ( let i = 0; i < 5; i++) { var random = Math.floor(Math.random() * (arrayLength--)); newArray.push(arrayClone.splice(random, 1)[0]); //arrayClone.splice(random, 1) returns removed items, here we removed one item so we can simply get 0 element of removed item } console.log(newArray); – SaminatorM Sep 25 '21 at 05:41