Categories
arrays html javascript jquery

How to get a number of random elements from an array?

176

I am working on ‘how to access elements randomly from an array in javascript’. I found many links regarding this. Like:
Get random item from JavaScript array

var item = items[Math.floor(Math.random()*items.length)];

But in this, we can choose only one item from the array. If we want more than one elements then how can we achieve this? How can we get more than one element from an array?

6

201

Try this non-destructive (and fast) function:

function getRandom(arr, n) {
    var result = new Array(n),
        len = arr.length,
        taken = new Array(len);
    if (n > len)
        throw new RangeError("getRandom: more elements taken than available");
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
}

13

  • 41

    Hey man, I just wanted to say I spent about ten minutes appreciating the beauty of this algorithm.

    Apr 8, 2017 at 9:57

  • @Derek朕會功夫 Ah, clever, that works much better for small samples from large ranges indeed. Especially with using an ES6 Set (which wasn’t available in ’13 :-/)

    – Bergi

    Aug 8, 2017 at 0:22


  • @AlexWhite Thanks for the feedback, I can’t believe this bug evaded everyone for years. Fixed. You should have posted a comment though, not suggested an edit.

    – Bergi

    Feb 20, 2018 at 17:22

  • 2

    @cbdev420 Yes, it’s just a (partial) fisher-yates shuffle

    – Bergi

    Sep 24, 2019 at 12:52

  • 1

    The jsPerf link seems broken at the moment.

    Jun 22, 2021 at 14:43

201

Try this non-destructive (and fast) function:

function getRandom(arr, n) {
    var result = new Array(n),
        len = arr.length,
        taken = new Array(len);
    if (n > len)
        throw new RangeError("getRandom: more elements taken than available");
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
}

13

  • 41

    Hey man, I just wanted to say I spent about ten minutes appreciating the beauty of this algorithm.

    Apr 8, 2017 at 9:57

  • @Derek朕會功夫 Ah, clever, that works much better for small samples from large ranges indeed. Especially with using an ES6 Set (which wasn’t available in ’13 :-/)

    – Bergi

    Aug 8, 2017 at 0:22


  • @AlexWhite Thanks for the feedback, I can’t believe this bug evaded everyone for years. Fixed. You should have posted a comment though, not suggested an edit.

    – Bergi

    Feb 20, 2018 at 17:22

  • 2

    @cbdev420 Yes, it’s just a (partial) fisher-yates shuffle

    – Bergi

    Sep 24, 2019 at 12:52

  • 1

    The jsPerf link seems broken at the moment.

    Jun 22, 2021 at 14:43

31

There is a one-liner unique solution here

 array.sort(() => Math.random() - Math.random()).slice(0, n)

2