Home » Php » php – How can i generate a 6 digit unique number?

php – How can i generate a 6 digit unique number?

Posted by: admin April 23, 2020 Leave a comment


How can i generate a 6 digit unique number? I have verification mechanisms in place to check for duplicate entries.

How to&Answers:
$six_digit_random_number = mt_rand(100000, 999999);

As all numbers between 100,000 and 999,999 are six digits, of course.


If you want it to start at 000001 and go to 999999:

$num_str = sprintf("%06d", mt_rand(1, 999999));

Mind you, it’s stored as a string.


Another one:

str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);

Anyway, for uniqueness, you will have to check that your number hasn’t been already used.

You tell that you check for duplicates, but be cautious since when most numbers will be used, the number of “attempts” (and therefore the time taken) for getting a new number will increase, possibly resulting in very long delays & wasting CPU resources.

I would advise, if possible, to keep track of available IDs in an array, then randomly choose an ID among the available ones, by doing something like this (if ID list is kept in memory):

$arrayOfAvailableIDs = array_map(function($nb) {
    return str_pad($nb, 6, '0', STR_PAD_LEFT);
}, range(0, 999999));

$nbAvailableIDs = count($arrayOfAvailableIDs);

// pick a random ID

$newID = array_splice($arrayOfAvailableIDs, mt_rand(0, $nbAvailableIDs-1), 1);

You can do something similar even if the ID list is stored in a database.


$file = 'count.txt';

//get the number from the file
$uniq = file_get_contents($file);

//add +1
$id = $uniq + 1 ;

// add that new value to text file again for next use
file_put_contents($file, $id);

// your unique id ready
echo $id;

i hope this will work fine. i use the same technique in my website.


Here’s another one:

substr(number_format(time() * rand(),0,'',''),0,6);


This will generate random 6 digit number



        $characters = '123456789';
        $charactersLength = strlen($characters);
        $randomString = '';
            for ($i = 0; $i < 6; $i++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];


In PHP 7.0+ I would suggest random_int($min, $max) over mt_rand().

$randomSixDigitInt = \random_int(100000, 999999);

From php.net:

This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.

So this depends mostly on context. I’ll also add that as of PHP 7.1.0 rand() is now an alias to mt_rand().



I would use an algorithm, brute force could be as follows:

First time through loop:
Generate a random number between 100,000 through 999,999 and call that x1

Second time through the loop
Generate a random number between 100,000 and x1 call this xt2, then generate a random number between x1 and 999,999 call this xt3, then randomly choose x2 or x3, call this x2

Nth time through the loop
Generate random number between 100,000 and x1, x1 and x2, and x2 through 999,999 and so forth…

watch out for endpoints, also watch out for x1


<?php echo rand(100000,999999); ?>

you can generate random number