# Outputting multiple items randomly from array

**URL:** https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631
**Category:** web dev
**Created:** [August 19, 2021, 4:07pm UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631 "2021-08-19T16:07:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Dadci\_Raouf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dadci_raouf/32/15163_2.png) [@Dadci\_Raouf](https://forum.kirupa.com/u/Dadci_Raouf)
#### Post date: [August 19, 2021, 4:07pm UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/1 "2021-08-19T16:07:27Z")

</div>

Hello everyone, I was wondering how we can output multiple items randomly from an array depending on an input and button, for ex: when we put 3 in the input and click on the button we display 3 random items on the screen, when we put 5 we get 5 etc, thanks !

---

<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: [August 19, 2021, 5:22pm UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/2 "2021-08-19T17:22:38Z")

</div>

Hi @Dadci_Raouf - thanks for posting here 🙂

Are duplicate items OK? Or should the items be unique?

---

<div class="post-metadata">

### Author: ![Dadci\_Raouf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dadci_raouf/32/15163_2.png) [@Dadci\_Raouf](https://forum.kirupa.com/u/Dadci_Raouf)
#### Post date: [August 19, 2021, 5:43pm UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/3 "2021-08-19T17:43:39Z")

</div>

Hi @kirupa thanks for your feedback  
The items should be unique yes with unique ID, for example you have a recipe blog, and you ask the user how many meals he wants to cook when he type 3 in the input you show him 3 random recipes !

---

<div class="post-metadata">

### Author: ![sephorus](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sephorus/32/15120_2.png) [@sephorus](https://forum.kirupa.com/u/sephorus)
#### Post date: [August 19, 2021, 7:56pm UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/4 "2021-08-19T19:56:48Z")

</div>

Hi @Dadci_Raouf. So we want to get N random recipes from our array where N is the value that users type into an input field.

I actually am curious as to the best way to do this because as I was writing out my answer, I realized that my strategy could have an infinite loop (hypothetically).

My original strategy was to keep an array of IDs we have already seen and iterate through it whenever we are trying to find a random recipe; however, once this additional array of IDs get sufficiently big enough, it would 1) be a pain to traverse through so many items and 2) hypothetically, the program could run forever if it continuously spit out a random recipe that we had already seen.

---

<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: [August 19, 2021, 9:03pm UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/5 "2021-08-19T21:03:29Z")

</div>

One approach that would work is what I have described here: [Picking a Random Item from an Array - #3 by kirupa](http://forum.kirupa.com/t/picking-a-random-item-from-an-array/399178/3)

😀

---

<div class="post-metadata">

### Author: ![Dadci\_Raouf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dadci_raouf/32/15163_2.png) [@Dadci\_Raouf](https://forum.kirupa.com/u/Dadci_Raouf)
#### Post date: [August 20, 2021, 5:14am UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/6 "2021-08-20T05:14:51Z")

</div>

Yes, the trick it can be not random, but the user will always have the same recipe or make the output random each time he chose the number and click the button.

---

<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: [August 21, 2021, 5:12am UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/7 "2021-08-21T05:12:26Z")

</div>

Did my earlier suggestion help, or are you still looking for some guidance? 🙂

---

<div class="post-metadata">

### Author: ![Dadci\_Raouf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dadci_raouf/32/15163_2.png) [@Dadci\_Raouf](https://forum.kirupa.com/u/Dadci_Raouf)
#### Post date: [August 28, 2021, 5:08pm UTC](https://forum.kirupa.com/t/outputting-multiple-items-randomly-from-array/648631/8 "2021-08-28T17:08:48Z")

</div>

Hey 👋 yes, but it’s more complicated,  
I decided to use a search bar instead, thank you so much 🙌🏼
