A
A
Alexey2020-02-17 11:50:11
Algorithms
Alexey, 2020-02-17 11:50:11

How to organize the generation of unique codes?

The problem is not only that it is impossible to generate unique codes, the problem is in additional generation. For example, you need to generate 500k codes, let's say there are already 400k in the database, the question is how to generate the required number of codes in less time. In order not to do 400k checks, to add 400,001 codes.

I apologize for not being complete. Codes can be of different lengths. The service also sets some rules for codes, for example, the length of the code and the alphabet.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2020-02-17
@xmoonlight

Use the formula: CODE=N.
Where code is the code and N is the serial number.
Or a more complex formula for code dependence on N without collisions.
Another option: to make a single 3-dimensional matrix for all existing codes for each character: character, position, code id.
Where empty cells remain, these are free combinations (acquaintance) for new codes.

R
Ruslan., 2020-02-27
@LaRN

You can try using a GUID. They are said to be unique. Those. you can not check for the presence of the same in the database when inserting a new one.
Well, or a unique code can be an integer. We find the maximum which is already stored in the database and start generating upwards from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question