Using the random item function

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?

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.

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.

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.

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

2 Likes

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

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

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

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

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

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).

1 Like

Give me your app name I’ll give it access

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!

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