Answer the question
In order to leave comments, you need to log in
Does it make sense to search for a similar randomly generated string?
Good afternoon.
I'm interested in such a question
Conditionally, I generate a random number
rand (0000000000,9999999999)
and now I'm wondering whether
it's worth going through all the lines in the database to find the same line?
I am afraid of the possibility that there is a meager possibility of generating the same string and this can harm the product
Answer the question
In order to leave comments, you need to log in
0) Describe the task, why do you need it?
1) Yes, maybe, and the probability is quite high. If it is fundamental to work with numbers, verification is mandatory.
2) To generate random keys, it is better to use a hash, not a digital key. Collisions are much less likely for him (although not zero, either).
3) Usually use pairs of values (for example, login and password) for greater uniqueness.
And the question remains open - why?
there is a meager possibility of generating the same string
If you need to generate a unique hash, then it is better to use uuid, and not to crutch. Efforts have already been made in the uuid generation algorithm to significantly reduce the risk of collisions.
You can put unique on this field in the database, then even if a collision suddenly happens, then at least the database will not be damaged by ambiguous data (but an error will occur, of course, which will need to be shown to the user).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question