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
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.

Commented:
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)
{
if(in_array(\$uniqueval,\$something)
{

}

}
0
Commented:
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.
0
All-around developerAuthor Commented:
I have 39 records in a mySQL table named Teams, the field is PIN.
0
Commented:
If you want a 4 digit pin, then rand(0,9999); needs to be rand(1111,9999);

<?PHP
\$pins = array();
\$maxPins = 39;

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

print_r(\$pins);
?>
0

Experts Exchange Solution brought to you by

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

ArtistCommented:
do{
\$newpin=rand()%1000000000;
\$result=mysql_query("Select * from Teams where PIN=\$newpin;");
}while(mysql_num_rows(\$result)>0);

At the end of this loop, you will have \$newpin equal to a random value that is not in the table.
0
Commented:
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.
0
ArtistCommented:
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.
0
Commented:
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.
0
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
PHP

From novice to tech pro — start learning today.