S
S
Sergey2015-01-13 18:56:25
PHP
Sergey, 2015-01-13 18:56:25

How to create a unique random value?

There is an online store in which an article from 100000 to 999999 is made through random.
The task, when adding a new product, is to automatically generate the next value of the article through random, but so that it is unique (only numbers).
How to implement this?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
H
Hazrat Hajikerimov, 2015-01-13
@hazratgs

Wait, why are you not satisfied with AUTO_INCREMENT in sql?
You will have to check with the base all the time whether there is one or not, the best option is id Primary AUTO_INCREMENT and the countdown would be from 100000.

A
Alexander Taratin, 2015-01-13
@Taraflex

We generate a hash from the characteristics of the product,
if a collision occurs, we take it randomly,
if a collision occurs, see step 2.

A
Alexander Aksentiev, 2015-01-13
@Sanasol

not random

#
# artur #, 2015-01-13
@passshift

And what AUTO_INCREMENT is not suitable? If not, then any random value can be added to it, in which case, if the random value matches, the final value will still be unique, you can also hash it from above ...

S
Sergey Melnikov, 2015-01-13
@mlnkv

And what, a random article looks cooler than a sequential one?

V
Valeriy Donika, 2015-01-13
@Valonix

I did this in OpenCart - I took the product id and added a rand () number to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question