Generate a unique 6 digit number

So I am trying to generate a unique 6-digit number. But just don’t know how to actually do it in Bubble.

I have read other forums that said compare this digit with others. And see if it already exists in database.

But they don’t actually show you how to do this. I have no idea how you will do this in Bubble.

image

This is what I currently have. There is a chance if this is rerun the same number will be generated. I want it to generate a new one if it already exists.

Hi there, @cobi… to make sure each class code is unique, you can use a recursive backend workflow. Here is an example that takes a class (which, in the example, is a custom data type) as a parameter.

The first action in the workflow makes changes to a class, and the change it makes is to generate a class code for the class.

The second action in the workflow schedules the workflow again if a count of a search for class codes that are the same as the class code that was just generated is greater than 0.

On the front end, you would create a new class in a workflow, and the next step in that workflow would schedule the backend workflow and pass the newly-created class to it. At that point, the backend workflow will take care of generating a unique class code.

Hope this helps.

Best…
Mike

3 Likes

Wow, absolutely fantastic. This is exactly what I needed thank you.

Just wanted to say In order to use API I had to purchase a paid account. I know it comes with the 2 weeks free trial but the app may take longer than this to develop. Is there any other ways I can do this without using the API, that you may be aware of.

image
little edit The count needs to be greater than 1. as the first step in the workflow will always generate 1 class. So no matter what it will always have 1 class with this code when comparing.

1 Like

Sorry, but nope.

1 Like

How many “classes” can there be? with 6 integers there’s only 5040 permutations.

I’m an idiot when it comes to that stuff, but Google tells me there are like a million unique combinations of 6 digits. Maybe I don’t know how to Google either, though.

1 Like

not that much classes. Less than 5040.

There is a possibility of 1,000,000. How did you get 5040 anyways.

There’ s no way. 6 digits of possible 0-9

You’re not arguing with me, Doug… you’re arguing with Google. :slight_smile:

3 Likes

Google always wins.

But MATH

10!/(10-6)! = 10!/(4)! = 10!/(4*3* 2*1) = 10* 9*8* 7/(4*3* 2*1) = 5040

1 Like

I literally have no idea what that formula means. Please refer to my aforementioned idiocy when it comes to this stuff, hence why I rely on the Google.

1 Like

Google is probably right anyways.

1 Like

Yeah there’s exactly 1 million unique 6 digit numeric strings:

000000
000001

999998
999999

And I suspect that @cobi doesn’t really mean to convert these to numbers. They should be strings (“texts”). You’ll note that if your generated ID has leading zeros those will disappear of course when converted to an actual number, so unless you catch that some of your IDs will have fewer than 6 digits.

AND, here’s a way to do the same thing @mikeloc did, but in the page…

There’s no “real” reason you couldn’t do this kind of recursion in the page using Custom Events (assuming that the privacy rules allow the page to see all Classes and their Class Code field), except that Bubble prohibits custom events from directly or indirectly calling themselves.

Of course, you won’t be surprised we could work around that with List Shifter if we wanted to do it in the page. The basic technique is to use List Shifter’s Custom Value (which is like a custom state, but also can fire an event when it’s updated) to hold the generated “class code”.

Here’s how to set up List Shifter (I used number in my example, but as I said these should probably be strings):

And then also set List Shifter’s Process Output Type (which also controls the type of the Custom Value) to number as well, lower down in the main property inspector:
image

Create a Custom Event that represents the Class Code generation:


:point_up:you’ll note I included a parameter for the list of existing Class Codes to make this simpler

And then the workflow for this event simply:


:point_up: set List Shifter’s Custom Value to the generated class code, make sure that the Publish and Trigger Event options are set to yes

And now set up 2 workflows - 1 to handle the case where the Class Code did not collide, and one to handle the case where the generated Class Code already exists:

Here’s the collision case:

And the workflow is just trigger Generate Classcode again:

Here’s the non-collision case:

And the workflow for that case is just now create (or in practice, perhaps modify) our Class using our Class Code:

And there ya go - in-page recursive workflows with List Shifter.

Live example with editor viewable (click button) here: https://list-shifter-dev-test.bubbleapps.io/version-test/classcode?debug_mode=true

That page is currently set to generate 3-digit codes so you can sometimes generate a collision. When that happens, you’ll note that Debug Buddy logs a message to the console. Of course, eventually there is a risk of infinite recursion once all Class Codes are used up. One would probably want to use a second List Shifter or Custom State to increment a number so that you put a max number of regenerations and break out of the recursion if the namespace fills up…

3 Likes

Google always wins.

But MATH

10!/(10-6)! = 10!/(4)! = 10!/(4*3* 2*1) = 10* 9*8* 7/(4*3* 2*1) = 5040

Actually Math(s) always beats Google… except that’s the wrong Maths for this situation…

The correct maths here is simply 10^n, where 10 is the number of possible values each character can be (0 to 9) and n is the number of characters.

i.e. each character can be a value 0-9 (so 10 possible variations), and there are 6 characters, so 10^6 or 10x10x10x10x10x10 = 1,000,000

Or, as @keith points out, the more intuitive way to establish it is simply by knowing that the largest possible 6 figure number is 999,999 (with the lowest being 0) which means there are precisely 1,000,000 possible combinations of a 6 numerical-character string.

There’s no “real” reason you couldn’t do this kind of recursion in the page using Custom Events (assuming that the privacy rules allow the page to see all Classes and their Class Code field), except that Bubble prohibits custom events from directly or indirectly calling themselves.

FYI… although Bubble prohibits Custom events from ‘Triggering’ themselves (even indirectly), there is no such prohibition on Custom events indirectly ‘Scheduling’ themselves, even with a scheduled time of 0 seconds…

Although I’ve no idea if that’s intended by Bubble, or of it’s one of those ‘loopholes’ which aren’t meant to be there, but are (and could be ‘fixed’ at any time)… but as it stands, it is possible to create workflow ‘loops’ on the page using 2 ‘scheduled’ (not triggered) custom events calling each other.

Still, no doubt ListShifter is still a better/simpler/more reliable option.

3 Likes

That’s super interesting, @adamhholmes! TIL

1 Like

Now I feel even dumber for having to Google how many unique combinations there are of 6 digits… thanks, Keith and Adam! :wink:

2 Likes

@mikeloc literally laughing out loud. :rofl:

2 Likes

Here’s a (ridiculously) simple example of looping Custom Events on the page using ‘Schedule’ (rather than trigger)…

Custom Event Loop (bubbleapps.io)

3 Likes

My own example could have used more of List Shifter’s features. For example, why not hold the search for Class Codes in List Shifter and just reference List Shifter’s Original List in those places where I wrote the Search expression?

(Answer: It’s Friday and I’ll do whatever I want. But also, it’s pretty much the same thing.)

2 Likes

I had no idea that was possible… thanks for real this time, Adam!

2 Likes