Generate unique 4 char values

I need to generate some PINs for a login system. I only have 39 values but I would like them to be unique,
how would I do that in PHP?
LVL 27
Eddie ShipmanAll-around developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There are several ways.

Are you saving the pins somewhere to make sure that they are not reused?
If so - we could do something like this:

$uniqueval = rand(0,9999);
/// do some code to get a list of previously assigned pins (not shown)
// (now do a while loop to go through the previously assigned vals)
while($x = 0)


oops hit a button on the keyboard and it submitted - try again

if(in_array($uniqueval,$something) {   $uniqueval=rand(0,9999); $x =0; }
else { $x = 1; }

At the conclusion of the loop $uniqueval should be a unique 4 digit value not previously assigned.
Eddie ShipmanAll-around developerAuthor Commented:
I have 39 records in a mySQL table named Teams, the field is PIN.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

If you want a 4 digit pin, then rand(0,9999); needs to be rand(1111,9999);

$pins = array();
$maxPins = 39;

for ($idx=0; $idx<$maxPins; $idx++) {
      $unique = false;
      while (!$unique) {
            $tmpPin = rand(1111,9999);
            if (!array_search($tmpPin, $pins)) {
       // you can update a table row $idx here


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cornelia YoderArtistCommented:
                $result=mysql_query("Select * from Teams where PIN=$newpin;");

At the end of this loop, you will have $newpin equal to a random value that is not in the table.
Why do multiple database lookups, when you can determine the uniqueness in code alone, and only hit the database when you update a record with the new PIN.

Also, (although not explicitly stated by the asker,) a PIN is normally a 4 digit integer, where rand()%1000000000; will return many different lengths.
Cornelia YoderArtistCommented:
OK, so just change the rand statement to generate whatever size number you want for the pin.

However, unless the pins are all generated at the same time and only once, you cannot save them in code.  You will have to do database lookups each time you generate a new one for a new user.
He says he already has 39 rows, so only hit each row one time, e.g. to update it with the unique PIN.

If he was adding a new row subsequent, then your query until unique would be a good choice.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.