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%.
@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.
@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.
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?