• 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

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.

  • 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);
?>

  • 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
  • 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:

  • 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

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

  • 0
  On 24/08/2015 at 12:21, Osy Odife said:

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
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • If you use AdGuard on your PC, you can find the new "Disable Windows Recall" feature in Settings > Tracking Protection. The "Disable Windows Recall" feature in Adguard 7.21 in turned on by default. No need to do anything if you want to use the feature.
    • Saw this on Twitter/X. Can't imagine the emotional rollercoaster going on inside of her head.
    • It's telling how brainwashed  and triggered you are that you immediately think it's because he was MAGA.    There's tons of things he's done throughout his wrestling career alone that wrestling fans right wing/ or left wing have problems with.  Not to mention his personal life. 
    • UK enforces strict new online age checks today by Paul Hill As of today, July 25, 2025, new Ofcom regulations mandate that “highly effective” age checks are in place for online services. These new rules apply to any websites or platforms that host pornography, self-harm, suicide, or eating disorder content. Major platforms like Pornhub, Bluesky, Discord, Grindr, Reddit, and X have committed to implementing age-gating. These age checks are part of the broader Online Safety Act, which is designed to make the internet safer, particularly for kids. These measures move away from just confirming you’re 18 by clicking a button to having to actually verify your age with ID or a face scan, but this move is not without its critics. Starting today, Ofcom will actively check compliance with the new rules and start launching investigations into non-compliant services, starting next week. The current enforcement program that’s focused on studio porn services is extending to all platforms allowing user-shared pornographic material, not just those websites dedicated to that. Ofcom is also launching another enforcement program that will target websites specifically dedicated to harmful content like self-harm, suicide, eating disorders, and extreme violence/gore. Ofcom has strong enforcement powers under the Online Safety Act, it can dish out fines of up to 10% of qualifying worldwide revenue or £18 million. For the worst offenders, Ofcom can even get websites blocked in the UK. Ofcom is already investigating 11 companies that it doesn’t think are following the rules. Aside from age checks, Ofcom’s Codes also require websites to protect children from dangerous stunts, misogynistic, violent, hateful, or abusive material, and online bullying. Algorithms of social media will need to be configured to block harmful content in children’s feeds, for example. Ofcom will be launching an extensive monitoring program requiring risk assessments by August 7 and practical action disclosures by September 30. While some have criticized the Online Safety Act, research cited by Ofcom shows that 71% of UK parents think the changes will positively impact children’s online safety, with 77% being optimistic about the age checks specifically. With that said, a significant minority of parents (41%) are skeptical about whether tech firms will follow the rules. Source: Ofcom | Image via Depositphotos.com
    • Microsoft has known to toggle settings in Windows Updates.
  • Recent Achievements

    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
    • One Month Later
      Philsl earned a badge
      One Month Later
    • One Month Later
      armandointerior640 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      624
    2. 2
      ATLien_0
      240
    3. 3
      Xenon
      163
    4. 4
      +FloatingFatMan
      124
    5. 5
      neufuse
      123
  • Tell a friend

    Love Neowin? Tell a friend!