Avoiding duplicates on a randomstring?

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 :grin: , 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?

I’m curious what this is for?

like a coupon, giftcard

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

1 Like

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.

agreed. not less WU, just faster for the user as you aren’t waiting for a search to complete

You’re generating millions? Damn, you must be raking it in!

The search is basically instant and probably quicker than running a DB trigger and running multiple actions to set and check slugs

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.

1 Like

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

169119-047499-088556-155586-556720-0
16911-90474-99088-55615-55865-56720-0

3 Likes

@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!

1 Like

Well, that’s quite nothing, you’re right. Sorry, I’m too lazy to make any estimations :slight_smile:
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.”);
}

const firstHalf = BigInt(strNum.slice(0, 16));
const secondHalf = BigInt(strNum.slice(16));

const result = (firstHalf + secondHalf) % BigInt(Math.pow(10, 15));

return result.toString().padStart(15, ‘0’); // Ensure it’s always 15 digits
}

// 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?

1 Like

To quote ChatGPT, “It will have to be an astronomical number of iterations”. Never is a misnomer, my bad.

It would be better if JS could just apply a calculation on a 32 digit number but that’ll not be practical.

Well if you want to make it a Never, adjusting the formula to include using a UNIX timestamp will work better since well, you cant reverse time.

Here’s a simpler function using a random number and UNIX to generate a 15 digit number.

function generateUnique15DigitNumber() {
// Get the current UNIX timestamp in milliseconds.
const timestamp = Date.now().toString();

// Generate a random 3-digit number.
const randomCounter = Math.floor(Math.random() * 1000).toString();

// Combine the timestamp and random counter.
const combined = timestamp + randomCounter;

// Return the last 15 digits of the combined string.
return combined.slice(-15);
}

// Example usage:
console.log(generateUnique15DigitNumber());

Again, not tested.

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!

Okay? :roll_eyes:

That’s true to any number with a set length. It’s just a gift code in this case. Every issued gift code should come with an expiry.

If OP wants to generate millions then it should come with 2 sets of numbers, like you get in some store gift cards.

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