Generate random combination of letters and numbers fith fixed length

Implementing this.

The first example makes sense. But the ‘trimmed from end’ works backwards. So it starts and the end and cuts the front of till it is only the last letters showing. But, there is a ‘truncated’ option that works as described.

Unfortunately, the 2nd part regarding the sequential ID numbers isn’t working so well. I tried to do a search for customer id’s (the name of the field to modify) to yield the ‘max’. Then added +1 as you described. I have another user as the ‘anchor’ at 999, so the next user should be 1,000. However, it just ends up being 1. Here are screens of the set up below:

I tried with ‘last item’ as described on this post as well, still no luck. Same result.

The first example makes sense. But the ‘trimmed from end’ works backwards. So it starts and the end and cuts the front of till it is only the last letters showing. But, there is a ‘truncated’ option that works as described.

yeah, oops. You can choose the one you like there :slight_smile:

Unfortunately, the 2nd part regarding the sequential ID numbers isn’t working so well. I tried to do a search for customer id’s (the name of the field to modify) to yield the ‘max’.

Hmm, I get it to work with your argument.

with both :last item and :max. Could you run through it again to see what’s up? I thought it had something to do with your thing to change but also works for me. I don’t know what your workflows look like before this one though.

It’s not a refresh DB issue?

2 Likes

oh wow. I don’t know. I’ll have to play with it a bit more.

I know that I had data imported with different customer IDs. that field I set to ‘text’. But I created another field (the one in the screenshot) which is set as a number. so that is not it. And I can’t see how it is a DB refresh issue since the ‘+1’ was added, but the ‘search for’ didn’t yield any result. thus the ‘1’.

I noticed that yours was called ‘customer IDnumber’ That shouldn’t matter as I am sure that is just how you labeled the field…

hmmm… I’ll have to play around a bit more.

thank you!

This is possible, if for example you recently added the field, or manually edited the “999” it wouldn’t have given the UI’s cached database values a reason to update.

Incrementing the max ID is a vulnerable method; when two users are created at roughly the same time they might return the same max ID, and caching makes the window of error even larger.

Note that each row already has a unique ID, that looks like:
1477501458054x757140743779018500

I take it you want something useful for people to read though!

Ideally it would be possible to use a database sequence, or PostgreSQL’s serial number type, to guarantee a unique number, perhaps the Bubble developers can make it available?

Edit: there is a workaround posted here, still allows collisions but less likely than if sequential:

Edit again: Could use Bubble’s random number generator function instead of calling the API.

1 Like

Correct. I tried both text format and number. FYI, :max applies to numbers only. :last you can use for both number and text. Your format should be number I assume. I noticed you also had that format defined.

Considering I got it to work in both cases, I’m thinking it must be other error. I’m uncertain of the rest of your setup ofc.

If you’re not going to use the sequential number for something that “requires” you to, I’d much rather just use the “random string generator”, like @mishav says. By doing so, you’re pretty safe from errors.

1 Like

So, @mishav may have hi the nail on the head. I I manually entered the field recently, and added the 999. So perhaps that is it. However, if I sign up another user, it still ends up being ‘1’ as well, instead of ‘2’. Not sure if that affects the theory at all.

Yeah, this is interesting. For our convenience, it would be easier to have it sequential, just for knowing how many customers we have. And later on, how many orders have been fulfilled.

Maybe I am misunderstanding how bubble arguments work, but I don’t think we could use the unique ID any way, as I would need to use this information for dynamic data. So I couldn’t use the unique ID as a customer ID, because I then wouldn’t be able to get workflows to add this to invoices later, correct?

The problem I am seeing with ‘random strings’ is duplication. Wouldn’t I have to set up a bunch of other workflows to check that it is unique?

While checking for uniqueness would be a bit more complicated that anything else I’ve done with bubble so far, I could see away. Just search for the ID, if it is found, repeat the function. I guess I don’t see how to keep this cycle going until it finds one the is unique, if many cycles happen to be needed.

The nicest solution is to assign the creation of the unique ID to a single source, which usually means the database as that’s where the value will be stored anyway.

The creation of the ID could be delegated to somewhere else, and retrieved via an API, for example using the database API to an external database which returns the next value of a sequence.

Allowing collisions and retrying is a valid solution, but the logic is messier to handle the collision situations and possible multiple retries.

1 Like

Well that kind of puts a dent in that theory (ofc I’m not saying it’s wrong). You tried both manual entry and signing a user up from the front-end, same result. I signed my users up from front-end but I manually changed the ID value to check the 999-1000 effect. I guess there’s something on your side. You could share your editor if you’d like someone to check it further.

Maybe I am misunderstanding how bubble arguments work, but I don’t think we could use the unique ID any way, as I would need to use this information for dynamic data. So I couldn’t use the unique ID as a customer ID, because I then wouldn’t be able to get workflows to add this to invoices later, correct?

Sure you can. You can always cross-reference data if you set your DB up correctly, where you define a new field as type [other field], e.g.
I have orders that are based on creations, that are created by users. So if you set it up correctly, you can use thing [Order]'s Creation’s user and all their fields respectively.

The problem I am seeing with ‘random strings’ is duplication. Wouldn’t I have to set up a bunch of other workflows to check that it is unique?

I don’t think so. Not exactly sure how to set it up just thinking about it now when reading your post but you should definitely be able to solve it. I think @mishav’s third party suggestion is valid. If you want to be really sure, perhaps there’s a possibility to use UUIDs through an API somewhere. You can read more here. (I think especially the part with

In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%.)

helps to put the duplication error worries to rest.)

If you’d go with bubble’s “generate string”, I guess it’d be up to @emmanuel to comment on the random string generator’s technical specs of generating randomness. However and from a basic probability point of view, the more character’s you use, the less chance of duplication. In addition, the actual probability of duplication when generating a random string with enough characters, using letters, non-cap letters, numbers and special characters is "practically 0-ish". :wink:

2 Likes

That’s correct. The algorithm is pseudo random and cannot guarantee strict randomness. And definitely cannot guarantee unique strings (not sure how that would work…)

1 Like

This is all really interesting. One of the hindering details is human readability/utility.

For example, if a user needs to search customers by ID. In this situation, more variables (letters, caps, length, etc.) that increase unique probability, would at the same time make data entry more difficult.

I am wondering if sequential numbers should be a function in menu dropdowns?

For the discussion of unique IDs, it would be nice, but not required. However, anything dealing with finance or accounting, this is necessary. Some countries require it for tax purposes, and eventually, ill need to do it for invoices. Super important to have sequential invoice numbers for order tracking in our industry.

I have never known what your app was about. :slight_smile:

If your talking about a user ID number, I’d say you could go with whichever solution that suits your needs. Instead of searching by ID; first name last name or email or something would be a lot easier. However, that doesn’t mean you can’t use the ID’s for many other things.

For building a finance system, the requirements are obviously tougher. I’m in EU and I know by law you’re required to sequence your invoices. It’s not a problem. I made it work. You just have to look through your app to find the error giving you the result of 1, and not the sequence.

1 Like

Great thread. There was some mention of ensuring uniqueness and to achieve that with a workflow. However, I couldn’t find any solution herein.

I am generating a random string once per user. It is intended that the string is unique to that user and will never be assigned to anyone else. At the point of generation, I want to be able to check all previously generated strings to ensure that the new one is in fact unique. If it is not, then I want to re-generate it until it is unique and then assign it to the current user.

Any thoughts?

If Bubble’s random function is called multiple times within the same workflow, the same string is returned each time, perhaps by a huge coincidence.

If calling an external API, such as mathjs or random.org, there might be a noticeable delay if the user is waiting on several API calls sequentially. A set of random strings could be retrieved at the same time into a list (not shareable with other users) to make regeneration more efficient.

Having a list of randoms might also make the retry loop easier.

Then there is the corner case where every random in the set has already been taken, do you start over, have a retry limit, or give the user a retry button, or abort and abandon the death star?

Hey all, regarding this topic, please check out a plugin I developed last night:

All the best.

Did you ever figure out how to create unique IDs that don’t duplicate? I’ve tried “when condition is true” workflows, recursive custom workflows (throws a bubble system error) and backend APIs to no avail…

2 Likes

Struggling with this at the moment too. I am generating strings of 4 characters including letters and numbers. If I’m correct that means there’s roughly 1.6 million combinations, however I can’t find how to ensure uniqueness…

Anyone? :smiley:

I ended up creating my own solution and documented the entire process here: Creating random unique IDs that do not duplicate

1 Like

I am still scratching my head to have unique random numbers between a start series say 12000 and end series 12500, @daniel10 had it nicely setup, however as @mishav said that random numbers throws repeated numbers more often than should be.

Can you simply have a series running between 1-500 and then add 12000 to it after the fact?

1 Like

Wow Smart ! @daniel10 Experience counts, and happy to see experienced people coming to help new bubblers.
I appreciate your time.
Thanks again