Random item in database based on chance?

Hello!

I’m trying to figure out how to implement a simple “loot box”-style system where each item has a certain chance of being selected, and one item is picked based on those chances.

For example:

Rare Box:

  • Hat (common 40% chance)
  • Pants (uncommon 40% chance)
  • Shirt (rare 20% chance)

I read that this could be done using JavaScript, but I’m not sure how to approach it.
Would really appreciate any tips or examples of how you’ve implemented something like this!

Just a 1 min thought, so there might be best things to do.

You could attribute each item a numeric range according to their probability. Then when you need to pick which item is won, you just have to get a random number and see in which range it falls.

Hey, thanks for your comment. But then it’s not based on the precentral chance sadly

Yes it is if the number is randomly picked between 1 and 100.

That would be the equivalent to a lotery where each item has multiple tickets.

  • Hat (has tickets from 1 to 40)
  • Pants (has tickets from 41 to 80)
  • Shirt (has tickets from 81 to 100)

You can do that with much numbers as you want as long as you randomly pick the winning number between 1 and the maximum number.

In the example, you randomly get a number between 1 and 100. And you see in which item range it falls.

1 Like

hmm okey so it would be a range and not a number? but in bubble generate random number you can’t select 1-100 correct? How would the workflow look like?

And lets say there are 3 items in one box that are Common, how to select a random one of these?

Type Chance % Number of values (1–100) Interval
Common 65 % 65 values 1–65
Uncommon 24 % 24 values 66–89
Rare 7 % 7 values 90–96
Epic 3 % 3 values 97–99
Legendary 1 % 1 value 100

@bonjour_17 thank you so much for pointing me in the right direction!
It was actually way easier than I thought thanks to your help, big thanks! :folded_hands:

For anyone else who might want to implement this, I’ll show how I did it below :backhand_index_pointing_down:

:yellow_circle: Step 1: Define your Rarity Ranges

Each item I have has a rarity RANGE that defines the probability of being picked:

Type Chance % Number of values (1–100) Interval
Common 65 % 65 values 1–65
Uncommon 24 % 24 values 66–89
Rare 7 % 7 values 90–96
Epic 3 % 3 values 97–99
Legendary 1 % 1 value 100–100

:yellow_circle: Step 2: Generate a random number

First, install the Random Number Generator plugin.
This will allow you to generate a random number within a given range, in my example, 1–100:

:yellow_circle: Step 3: Filter your items based on the random number

Then I simply Do a Search for (the thing you want to be randomly picked), filter it based on your needs, and set the rarity_range to contains (point).

This tells Bubble to pick an item whose rarity range includes your random number.

Example:
If the random number is 92 → a Rare item will be picked (since Rare covers 90–96).

:yellow_circle: Step 4: Pick one random item (if you have multiple with same rarity)

Since you might have multiple items with the same rarity, just add :random item at the end of your search.

This ensures you get one random item from the correct rarity pool:

I hope this helps someone else who was struggling with this like I was! :slightly_smiling_face:
It’s a really simple way to build a nice rarity-based random mechanic in Bubble.
Works great for games, collectibles, rewards, you name it!

If you have any questions just post them below🙂