I need to generate millions of random, unique, and digit-only codes in PHP with the fix length of 14. Is there any way to do so? I considered using microtime, but the length of numbers may vary.
Asked
Active
Viewed 640 times
-1
-
1Out of curiousity, why restrict yourself to numbers? I mean, you could do something like [this](https://stackoverflow.com/a/31107425/899126), but if you're looking for pretty much guaranteed uniqueness, you'll have a better time with a [uuid](https://github.com/ramsey/uuid) – Chris Forrence Aug 21 '18 at 21:28
-
@ChrisForrence Thank u for ur comment. I need numbers due to a kind of customer's protocol. I cannot use other characters. – Reza Kazemifar Aug 21 '18 at 21:30
-
Do you have a user id or something unique to add to the mix, random generators cannot be unique every time over a long period of time. – Ice76 Aug 21 '18 at 21:31
-
@IdontDownVote Will this always result in 14 digit codes? – Reza Kazemifar Aug 21 '18 at 21:33
-
@Ice76 This is why I need to do this using microtime. Microtime seems to guarantee the uniqueness over long periods of time... – Reza Kazemifar Aug 21 '18 at 21:34
-
@IdontDownVote You are damn right :-) I preferred a quicker approach :-P – Reza Kazemifar Aug 21 '18 at 21:35
-
@RezaKazemifar Micro time wont work as time continues to increase – Ice76 Aug 21 '18 at 21:35
-
@RezaKazemifar and what if two are required to be made within the same second? – Ice76 Aug 21 '18 at 21:36
-
@Ice76 Yep I had cases where 8 were generated within a second. But I think microtime is one millionth of a second and there should be a way to use the micro seconds, huh? – Reza Kazemifar Aug 21 '18 at 21:39
-
@IdontDownVote I tested your solution. The length varies and produces something like this: 1.6094913158472E+15 :-( – Reza Kazemifar Aug 21 '18 at 21:45
-
@IdontDownVote I should give it more tries as I fear damaging its uniqueness by trimming its length... – Reza Kazemifar Aug 21 '18 at 21:55
-
2There isn't a way to ensure both randomness and uniqueness in one step. True randomness cannot guarantee uniqueness (because that would mean it wasn't truly random). If you want uniqueness, you will just need to check if the generated value is already in your store of existing codes and regenerate if it is already used. – Anthony Aug 21 '18 at 22:01
-
@Anthony Thank you Anthony... – Reza Kazemifar Aug 21 '18 at 22:12
-
If you want something that is likely "random enough", you could probably use part of microtime and append a random value. You still can't guarantee uniqueness without checking, but you can get close. This would ensure that you get an incrementing number and if multiple ids are generated in the same increment, the random value appended would also have to be the same. This is kind of how guids can be unique and random with a really low chance of collision. However, 14 digits isn't really that long to guarantee either. – Jonathan Aug 21 '18 at 22:22
-
Also, it is necessary to know if you want it to be a 14-character string, or a 14 digit integer. If '00000000000001' isn't a valid code (because you don't want the leading zeros), then that would reduce your potential set from 99,999,999,999,999 (100 trillion) unique codes to 90,000,000,000,000 (90 trillion). – Anthony Aug 21 '18 at 22:22
1 Answers
0
Like this, maybe?
function generateByLoop()
{
$string = '';
for($i = 0; $i < 14; $i++) {
$value = (string) random_int(0,9);
if ($i === 0) {
$value = (string) random_int(1,9);
}
$string = $string . $value;
}
return $string;
}
Seems a bit of over-engineering if you ask me, but well. And here's another solution based on microtime (which does not involve randomness, but how fast can it be used?)
function generateByMicrotime() {
$microtime = microtime(true);
$microtime = str_replace('.', '', $microtime);
echo (substr($microtime, 0, 14));
}
Though I must agree with the argument by @Anthony that:
True randomness cannot guarantee uniqueness.

Rafael
- 1,495
- 1
- 14
- 25
-
-
The chances of the string NOT being unique exists, but then you could set this in a function looping the output and checking if this actual string does exist in your database and try again. I'm sorry, I cannot come up with any other idea now. – Rafael Aug 21 '18 at 22:12
-
-
Glad I could help. :) Another option, though, would be using miliseconds (since they roll up to 13 digits) and generating the last one randomly, that should be as close to unique as I can think. – Rafael Aug 21 '18 at 22:14
-
1How is this any different than just doing a single call to random_int? For example: https://3v4l.org/2SrZt – Jonathan Aug 21 '18 at 22:28
-
Nothing really, I just wanted to generate digit by digit which might actually consume more to do than your solution. It probably consumes more. Great solution @Jonathan! – Rafael Aug 21 '18 at 22:43
-
@Jonathan Thank you man! BTW, what is "cryptographically secure"? – Reza Kazemifar Aug 22 '18 at 08:03
-
@RezaKazemifar There are randomly generated values that are used when encrypting data. These values need to be as random as possible to prevent someone from guessing that value. If someone could guess the random value, they could essentially guess the key to decrypt it by repeating whatever method was used to generate the random value in the first place. Having a "cryptographically secure" random generator makes it less likely that the random values can be guessed. – Jonathan Aug 22 '18 at 16:22
-
@Reza. If you are satisfied with the answer, upvote and choose the answer, mate. :) – Rafael Aug 22 '18 at 17:43
-
-