Hey guys. So, I have this card that stores a random 15 numeric digit value, where I need it to also be a unique value.
The problem is that I generate that card on a backend workflow loop that I need to keep it fast, and the only idea that came to mind was to create that card with the random, then make a search, then create another card if its duplicated.
Is there a proper way to validate this randomstring as unique on the go that I’m not thinking of?
I know this answer will offend someone but with a 15 digit number I wouldn’t even bother checking for duplicates. Assuming you’re not starting your number with 0 (which is what you should do), you have 10^14 possibilities. The probability of generating a duplicate is negligible.
Also, if you do include the search (which I believe is the only way you can really check), it will only run once (given that you won’t find a duplicate anyway) so shouldn’t be too WU costly.
EDIT: before the maths guy comments about the birthday paradox , the probability of at least one duplicate integer if you generate the unique number 1,000,000 times is 0.5%.
I’m currently betting on that, with privacy rules. So, the duplicate would have to exist, and exist on the same exact client. Although we will have millions of this record.
I’ll include a search for intersect with for verification in the night to keep a look at that. Do you know the best way to do that search for duplicates?
@vitor370 , I’m not saying its necessary, but if you want to make certain that there are no duplicates, I’d make a DB trigger that, upon a new coupon being created, checks if the coupon’s 15 digit code can be a valid slug.
If it’s a valid slug → it’s unique.
If it’s not a valid slug, trigger a WF to reset the code.
This is a cool idea, but I’m doubtful that the actions of 1. setting slug for new coupon 2. initiating DB trigger 3. attempting to set slug (to check it’s unique) will use less WU than a simple search. Can definitely try it out though.
I’ve actually did kind of that way. I’ve setup the thing slug, and then I scheduled another workflow to search once a day for things with empty slugs (which should never occur if not duplicated)
I’m setting up the database for the client that say he’ll have millions of that. He has a huge corporate client.
This isn’t 15 characters but use real Bubble unique IDs (by Creating a thing and getting the result’s uid) but remove the “x” and split it into human-manageable sections
@georgecollier, 0.5% probability is high if you’re sensitive to duplicates. Someone need to resolve 500 expected duplicates that will occur when generating 1M cards.
But searching for a duplicate is fast, I agree. Searching among 1M records is done with 1K operations, so I’d just check it on card creation and recreate if needed in a recursive loop. In the worst case you can limit number of tries in a recursive loop and say “Oops, try again”.
No, there’s a 0.5% chance of generating at least one duplicate if you generate the number 1 million times. So, if we generated distinct sets of 1 million numbers 200 times, we’d expect one of those 200 sets to have a duplicate.
The Bubble unique ID answer is the correct one but I assumed you didn’t want 32 characters for ease of use!
Well, that’s quite nothing, you’re right. Sorry, I’m too lazy to make any estimations
UID is actually the only way to guarantee uniqueness with vanilla bubble, so you have no other choice. Even checking the existance of generated sequence may fail because of race conditions.
Here’s a JavaScript function I got from ChatGPT4 that will take the UID (32 digits) and convert it to a 15 digit number using a mathematical formula. Haven’t tested it yet but looks legit.
function reduceTo15Digits(strNum) {
if (strNum.length !== 32) {
throw new Error(“Input must be a 32-digit number as a string.”);
}
// Example usage:
const random32DigitNum = “12345678901234561234567890123456”;
console.log(reduceTo15Digits(random32DigitNum));
What I’d do is just run this using Toolbox and grab the result using it’s JavaScript to Bubble element. It’s using one formula and since each UID is unique you’ll never run into a duplicate.
Can you explain how this works? Surely there must be a chance that two different unique IDs can produce identical 15 digit outputs, so it’s no better than calculating a random number. There are 10^32 possible 32 digit numbers, and only 10^15 possible 15 digit numbers, so shouldn’t there necessarily be some 32 digit numbers that will map to identical 15 digit numbers?
But for the 32 digit number, there are 10^32 possibilities. For 15 digit number, there are 10^15 possibilities. How could each 32 digit number have a unique 15 digit number? It’s just impossible haha!
Yeah of course, I’m just trying to work out why using the Bubble unique ID as a base to generate code would have any advantage over generating a random string