# Using the random item function

**URL:** <https://forum.bubble.io/t/using-the-random-item-function/281334>\
**Category:** Need help\
**Created:** [September 4, 2023, 10:49am UTC](https://forum.bubble.io/t/using-the-random-item-function/281334 "2023-09-04T10:49:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![loconft2](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@loconft2](https://forum.bubble.io/u/loconft2)\
**Post date:** [September 4, 2023, 10:49am UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/1 "2023-09-04T10:49:34Z")

</div>

I have a database that looks like the following.

User, Number of entries

Elliot, 1  
John, 4  
Rachel, 5

I need to pick a random item from the database and fairly account for every entry. When picking a random item from the above example, Elliot will only have one chance to be picked, John 4 chances and so on.

Can this be achieved?

---

<div class="post-metadata">

**Author:** ![georgecollier](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/georgecollier/32/182327_2.png) [@georgecollier](https://forum.bubble.io/u/georgecollier)\
**Post date:** [September 4, 2023, 11:23am UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/2 "2023-09-04T11:23:47Z")

</div>

This would be a lot easier if you created a data type of ‘Entry’ with a User attached. Then, search for Entries, sort randomly and pick the first one one the list. That sort might not be completely mathematically random but for all intents and purposes it is…

It shouldn’t be hard to change this. Whenever your app makes changes to user’s entries just create or delete an Entry instead.

---

<div class="post-metadata">

**Author:** ![loconft2](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@loconft2](https://forum.bubble.io/u/loconft2)\
**Post date:** [September 4, 2023, 11:44am UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/3 "2023-09-04T11:44:49Z")

</div>

> [@georgecollier](#):
>
> ch for Entries, sort randomly and pick the first one one the list. That sort might not be completely mathematically random but for all intents and purposes it is…
> 
> It shouldn’t be hard to change this. Whenever your app makes changes to user’s entries just create or delete an Entry instead.

Thanks for your response. Sorry but my database is set up as an entry with the user attached.

Unfortunately I need it to be mathematically correct by giving users with more entries a higher chance of being randomly picked and vice versa.

---

<div class="post-metadata">

**Author:** ![georgecollier](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/georgecollier/32/182327_2.png) [@georgecollier](https://forum.bubble.io/u/georgecollier)\
**Post date:** [September 4, 2023, 12:04pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/4 "2023-09-04T12:04:34Z")

</div>

> [@loconft2](#):
>
> Unfortunately I need it to be mathematically correct by giving users with more entries a higher chance of being randomly picked and vice versa.

Yes you can do that like I told you, but not really with your current database structure… your database is the limiting factor here… It will be mathematically correct if you revise your database structure.

> [@loconft2](#):
>
> Sorry but my database is set up as an entry with the user attached.

You can change it pretty easily for this case.

If you really don’t want to change it (which is the wrong solution), you need to do something along the lines of:

Calculate total entries  
Assign entry numbers (e.g Elliot has 1, John 2-5, and Rachel 6-10)  
Pick a number between 1 and the total entries  
Work out who has that number using a loop to check the sum of all previous entries in the list

---

<div class="post-metadata">

**Author:** ![mishav](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/mishav/32/6420_2.png) [@mishav](https://forum.bubble.io/u/mishav)\
**Post date:** [September 4, 2023, 2:03pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/5 "2023-09-04T14:03:16Z")

</div>

> [@georgecollier](#):
>
> Calculate total entries  
> Assign entry numbers (e.g Elliot has 1, John 2-5, and Rachel 6-10)  
> Pick a number between 1 and the total entries  
> Work out who has that number using a loop to check the sum of all previous entries in the list

I agree with this approach. The implementation depends on how many candidates and whether frontend or backend is required.

Here is discussion of a [similar situation with unequal probabilities](http://forum.bubble.io/t/get-random-value-based-on-probabilities/278701)

Edit - sometimes it seems a backend process is required, but could be done manually on frontend by a suitably responsible user, thus saving WU.

---

<div class="post-metadata">

**Author:** ![loconft2](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@loconft2](https://forum.bubble.io/u/loconft2)\
**Post date:** [September 5, 2023, 7:06pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/6 "2023-09-05T19:06:11Z")

</div>

> [@georgecollier](#):
>
> Work out who has that number using a loop to check the sum of all previous entries in the list

Thank you very much for your response. I would like to explore the possibility of using the last option. Please can you elaborate on how I can work out which user has which number via using a loop?

Many thanks,

Elliot

---

<div class="post-metadata">

**Author:** ![chris.williamson1996](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/chris.williamson1996/32/70581_2.png) [@chris.williamson1996](https://forum.bubble.io/u/chris.williamson1996)\
**Post date:** [September 5, 2023, 7:26pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/7 "2023-09-05T19:26:30Z")

</div>

Bubbles random operations both :random item & random sort are not very random. Even with hundreds of options you usually end up with a few vastly over chosen values. (Especially bubble server side random sorting)

Dealt with this on my NFT generator. Ended up implementing chance.JS

---

<div class="post-metadata">

**Author:** ![loconft2](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@loconft2](https://forum.bubble.io/u/loconft2)\
**Post date:** [September 5, 2023, 7:38pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/8 "2023-09-05T19:38:44Z")

</div>

> [@chris.williamson1996](#):
>
> Dealt with this on my NFT generator. Ended up

Im not very familiar with how to implement JS. Any chance you can point me in the right direction?

---

<div class="post-metadata">

**Author:** ![georgecollier](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/georgecollier/32/182327_2.png) [@georgecollier](https://forum.bubble.io/u/georgecollier)\
**Post date:** [September 5, 2023, 8:13pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/9 "2023-09-05T20:13:34Z")

</div>

Yes I think Bubble docs say somewhere that it’s not true random - that’s what I meant by it not being completely mathematically random (true random).

---

<div class="post-metadata">

**Author:** ![chris.williamson1996](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/chris.williamson1996/32/70581_2.png) [@chris.williamson1996](https://forum.bubble.io/u/chris.williamson1996)\
**Post date:** [September 5, 2023, 8:55pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/10 "2023-09-05T20:55:34Z")

</div>

Give me your app name I’ll give it access

---

<div class="post-metadata">

**Author:** ![loconft2](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@loconft2](https://forum.bubble.io/u/loconft2)\
**Post date:** [September 6, 2023, 5:54pm UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/11 "2023-09-06T17:54:17Z")

</div>

App name is Testjsmath.

In this example a button will need to look at raffle ID 1. Select a random winner based on the number of entries they have placed.

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/bubble/original/3X/2/8/2847faa74692c2ffa5d939d3dc3724cdd060d247.png) [@system](https://forum.bubble.io/u/system)\
**Post date:** [November 13, 2023, 10:49am UTC](https://forum.bubble.io/t/using-the-random-item-function/281334/12 "2023-11-13T10:49:53Z")

</div>

Closed on November 13, 2023, 10:49am UTC

This topic was automatically closed after 70 days. New replies are no longer allowed.
