# Picking a Random Item from an Array

**URL:** <https://forum.kirupa.com/t/picking-a-random-item-from-an-array/399178>\
**Category:** programming\
**Created:** [December 2, 2014, 12:27am UTC](https://forum.kirupa.com/t/picking-a-random-item-from-an-array/399178 "2014-12-02T00:27:34Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 20, 2015, 6:52am UTC](https://forum.kirupa.com/t/picking-a-random-item-from-an-array/399178/3 "2015-07-20T06:52:17Z")

</div>

One way would be to shuffle the array and then remove the first three items from it: [http://www.kirupa.com/html5/shuffling\_array\_js.htm](http://www.kirupa.com/html5/shuffling_array_js.htm)

That would be inefficient for large sets of data, so you could just literally remove three random items using something as follows:

```
var myShows = ['Bones', 'Psych', 'Big Bang Theory', 'Mad Men', 'Breaking Bad', 'Modern Family', 'Game of Thrones', 'Dexter'];

function getRandomIndex(items) {
  return Math.floor(Math.random() * items.length);
}

for (var i = 0; i < 3; i++) {
  var removedItem = myShows.splice(getRandomIndex(myShows), 1);
  document.writeln(removedItem)
}

```

Does this help? 😄

Cheers,  
Kirupa

---

_[View the full topic](https://forum.kirupa.com/t/picking-a-random-item-from-an-array/399178)._
