How to generate a random number between 1-4, without plugins

Good evening!

I was trying to generate a number between 1-4, but failed while trying to found one property or even a plugin.

Hmm :thinking:

Maybe put it in an option set and then use the random functionality that is built into Bubble?

Use Arbitrary text then type 1,2,3,4 :split by “,” each item :converted to number :random item

Bubble doesn’t have an obvious random number generator but here are two quick ways:

Easiest is the Random item from a list as told by @Newed
Create a custom state as a list of numbers 1, 2, 3, 4 and use :random item. Simple and reliable, no plugins needed.

Formula approach is also available:
In any expression field: 1 + (random number * 3) then :rounded to 0 decimals , gives a whole number between 1 and 4.

If you want pure JavaScript:
Install the free Toolbox plugin and use the Expression element with Math.floor(Math.random() * 4) + 1.

The list + :random item approach is the quickest to set up. Good luck @rodrigoandradkhomyk !

You can do this natively in Bubble without a plugin.

Use a calculated field with: Current date/time’s milliseconds :formatted as number then use the :mod operator with 4, and add 1 to the result. That gives you a number from 1–4.

Alternatively, create a list of numbers (1, 2, 3, 4) and use :random item to pick one — that’s probably the simplest approach in Bubble.