• 0

[PHP] How to generate unique random number?


Question

Hi,

I wanted to generate at least one thousand of unique 16-digit number using Random Number Function.

Like

1010101010000001

1234501010009801

9087651340298745

4390192836456106

I don't want the negative number. And I want the number to be exact 16-digit, for example, it should be 000000000002323, not 2323

I have scripted the code

echo "<table width=\"100\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
for ($i=0;$i<1000;$i++)
	{
echo "<tr><td>";
echo rand(0000000000000000,9999999999999999);
echo "</td></tr>";
	}
echo "</table>";

But I am not too sure on how to generate one thousand unqiue of 16-digit number using PHP code. Anyone of you who can help with this?

Thanks alot. :D

Oops! I realised that I posted in wrong section. Can any moderator please help moving this to correct section?

Thanks :blush:

Edited by Novawin
Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

The only way you can guarantee that the numbers you're generating will be unique is to store all of them in to an array, and then compare the newly generated random number against that array. If the number exists already, ignore it and generate another one, if it doesn't, add it to the array and then start again.

Link to comment
Share on other sites

  • 0
<?php
$max = 1000;
$digits = 16;

$numbers = array();
$power = pow(10, $digits);

for($i=0;$i<$max;$i++)
{

	$num = str_pad(mt_rand(0, $power), $digits, "0", STR_PAD_LEFT);
	if (!in_array($num, $numbers))
		$numbers[] = $num;
	else
		$i--;
}

print_r($numbers);
?>

Link to comment
Share on other sites

  • 0

Thanks all for your reply.

By the way, I tried to use array() inside PHP code as shown below

<?php

function number($length)
{
   $array = array();
   for($i=0;$i<$length;$i++)
	 $key .= rand(0,9);

   return $key;
}

echo "<table width=\"100\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
for ($i=0;$i<1000;$i++)
  {
  echo "<tr><td>";
  echo number(16);
  echo "</td></tr>";
  }
echo "</table>";

?>

This has met my requirement but some number are repeated. I want every number to be unique and it can't be repeated. Can anyone of you please help checking my code and try to modify as possible to only display every unique number?

Thanks alot! :D

Edited by Novawin
Link to comment
Share on other sites

  • 0

Never mind about this. I found out that after I uploaded the webpage to Linux Server, all 16-digit number are unique whereas this webpage being tested on localhost on my Windows laptop, some numbers are repeated. This is so weird... But it's okie anyway. Thanks all for your help. :cool:

Link to comment
Share on other sites

  • 0

it repeates due to it taking some information from the system clock IIRC. dont quote me on that but i think it takes one number from the system, which is sometimes repeated.

i think its something like that, and no matter what you do unless like stated you create an array then there is no way 2 generate a unique number everytime

Link to comment
Share on other sites

  • 0

I am new to programming. Please I need help. I want to use the above codes for my project but I only need 12 digits. When I change the digits =12, the code generated the following error ; Warning: mt_rand(): max(-727379968) is smaller than min(0) in C:\xampp\htdocs\ITB\RECOS\Theme\random.php on line 11

I do not have any idea of what that means.

Thanks in advance.

Link to comment
Share on other sites

  • 0

I am new to programming. Please I need help. I want to use the above codes for my project but I only need 12 digits. When I change the digits =12, the code generated the following error ; Warning: mt_rand(): max(-727379968) is smaller than min(0) in C:\xampp\htdocs\ITB\RECOS\Theme\random.php on line 11

I do not have any idea of what that means.

Thanks in advance.

<?php
$max = 10;

$digits = 12;


$numbers = array();

$power = pow(10, $digits);


for($i=0;$i<$max;$i++){
 $num = str_pad(rand(0, $power), $digits, "0", STR_PAD_LEFT);
 if(!in_array($num, $numbers)){
  $numbers[] = $num;
 }else{
  $i--;
 }
}


print_r($numbers);

?>

Here's the code corrected code for 12 digits, the problem was with the mt_rand function, it only works with 2, 4, 8, 16, 32, 64 etc digits.

Welcome to neowin :D

 

Edit: something doesn't seem to work correctly, rewriting the code.

Edited by Seahorsepip
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.