I need to create a completely random number for each user on my website and insert it into a MySQL database. The numbers can never repeat and I don’t want to use MySQL AutoIncrement because I need a specific range. Say from 111,111 to 999,999,999,999. No higher or lower as of right now. I do have a specific reason for that range, incase you were wondering. I know I can use the unique feature on MySQL but that only prevents me from inserting duplicates. I was thinking of having 2 functions, one to generate the number, and another to check if that number is used. They would keep looping until they found a number that wasn’t used. I know how inefficient that is and how long it may take once majority of those numbers have been taken so that is why I have come here, for a better answer. What do you suggest I do because I am all out of ideas. Thanks for any help!
I need to create a completely random number for each user on my website
Share
Assuming that we will make it possible to generate any value in the closed interval [111111,999999999999], then we merely need to find some integer that is relatively prime to
This a composite number, with rather large factors {18181, 55002469}.
So now build a simple, not terribly random (but surely adequate here) number generator that will generate a new pseudo-random integer from the last one chosen. Do that by a simple modular computation. Pick some number to start with, and another integer that is relatively prime to the factors listed above.
You can repeat this process without cycling until you get very bored.
Since you want the lower limit to be 111111, just add that value to every number when you use it. This procedure yields the following sequence:
You will add 111111 to every value to ensure that the lower limit is truly 111111, so the sequence that will be reported is just:
The nice thing is all you need to be able to do is store the last member of the sequence, and do modular operations on a number that will fit into a 64 bit integer.