I am looking to generate a random number for every user contribution as a title of the contribution.
I could simply check the database each time with a query and generate a number which does not equal to any of the entries of the database. But I imagine this as inefficient and it could become slow if the database is big in my opinion. Also I'd have to contain all the numbers of the database somewhere to manage the "not equals to", in an array or something similar but that can end up as a giant one.
Excuse the layman's speech I am new to this.
Any suggestions how this can be solved efficiently without straining the resources too much? You can explain it linguistically and do not have to provide me any scripts, I will figure it out.
You can use uniqid()
. I'm not sure how portable it is.
Example:
printf("uniqid(): %s
", uniqid());
Will output something like:
uniqid(): 4b3403665fea6
uniqid() will give you a random number that can technically repeat.
Maybe you can apply a simple algorithm on an auto-increment field? n(n+1)/2 or something?