# Phrase (Text) Random

**URL:** <https://forum.wixstudio.com/t/phrase-text-random/37085>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [September 9, 2020, 2:11pm UTC](https://forum.wixstudio.com/t/phrase-text-random/37085 "2020-09-09T14:11:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![contactodharti](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/contactodharti/32/23324_2.png) [@contactodharti](https://forum.wixstudio.com/u/contactodharti)\
**Post date:** [September 9, 2020, 2:11pm UTC](https://forum.wixstudio.com/t/phrase-text-random/37085/1 "2020-09-09T14:11:54Z")

</div>

I want a text to connect to a dataset and randomly extract a text.

Example:

1. I enter the ‘new page’.
2. The random text of the dataset is loaded.
3. Done! (If I go back into the tab, another text appears, NOT the same one).

All the tutorials I have seen so far have not been able to help me more. Is there any function or code that can take the mapped data in the database and randomize which input is selected?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Shan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/shan/32/106059_2.png) [@Shan](https://forum.wixstudio.com/u/Shan)\
**Post date:** [September 9, 2020, 3:16pm UTC](https://forum.wixstudio.com/t/phrase-text-random/37085/2 "2020-09-09T15:16:33Z")

</div>

One way to do it:

**Step 1.** Query the database for all the items (preferably on the backend)

**Step 2.** Generate a random number which **should be** less than the actual array length

```javascript
wixData.query('database_id').limit(1000).find().then( (res) => {
    let items = res.items;
    let randomNum = Math.floor(Math.random() * items.length) - 1;
    let randomItem = items[randomNum];
        console.log(randomItem); //this will be your random database item
 	$w("myText").text = randomItem.value //Set your value on the text element
});

```

---

<div class="post-metadata">

**Author:** ![contactodharti](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/contactodharti/32/23324_2.png) [@contactodharti](https://forum.wixstudio.com/u/contactodharti)\
**Post date:** [September 9, 2020, 9:25pm UTC](https://forum.wixstudio.com/t/phrase-text-random/37085/3 "2020-09-09T21:25:54Z")

</div>

Where do I put this code that you send me in the second option?

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [September 10, 2020, 7:58pm UTC](https://forum.wixstudio.com/t/phrase-text-random/37085/4 "2020-09-10T19:58:13Z")

</div>

In the page code panel
