• 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

    • I can't believe it was only two years since the last time MS raised their prices and now they think they can do it again so soon. Of course they needed to backtrack on this. I hope Nintendo is next.
    • I guess I gotta be even more specific then since I'm looking for an absolute confirmation.  The mini PC has never booted up or powered on by me. I'm assuming it'll be like buying a new PC from a store. Nothing is set up. Like absolutely nothing on the mini PC because I can't emphasize this enough. My USB drive already has Windows set up on it from my other desktop so I know it's clean. So now I connect it to the mini PC before it's even booted up or go to the desktop. I then turn on the mini PC and boot into the USB drive. I should see an existing partition. I'll delete it and then install Windows again over the entire drive. I don't necessarily want to create a separate partition. Once it installs and boots to the desktop, I'm assuming the drive is "clean" and free of any malware/viruses. Basically, this process will eliminate any potential for malware/viruses to still somehow hide in the SSD and then potentially affect me later.  I'm being very anal about this because I really don't trust anything bought from AliExpress, but the price was very tempting.  Is Windows Defender reliable? Afterwards, if I do a full scan and it doesn't find anything in the SSD and the USB drive, can I be confident the mini PC is now safe and secured to enter my banking credentials? If not, what else can I do to be absolutely certain?
    • I got an ESR Qi2 vent charger for my car and it's fantastic. It's very strong magnet and holds the phone securely over the worst potholes/roads, and my iPhone does charge. In Summer, the AC blows on the vent keeps and keeps the phone cold. In winter, I just close that vent, so it still stays cold. I agree with you on the Android front. Why not just build in the magnets like Apple does? It's frustrating to buy a case that adds magnets.
    • Hey, I've been using Windows since I was a little tyke. But for the last 15 years, I have been using Linux. I'm using EndeavourOS/Arch atm. wipe it, as in deleting anything from the SSD in the computer. Make a new partition. USB booting device should be free of malware, as long as you download it directly friom MS. Not some XYZ company.
    • "Nuking it" is slang for doing a clean install. USB installation media... delete the partition... start fresh.
  • Recent Achievements

    • Collaborator
      fernan99 earned a badge
      Collaborator
    • Collaborator
      MikeK13 earned a badge
      Collaborator
    • One Month Later
      Alexander 001 earned a badge
      One Month Later
    • One Month Later
      Antonio Barboza earned a badge
      One Month Later
    • Week One Done
      Antonio Barboza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      584
    2. 2
      ATLien_0
      219
    3. 3
      Michael Scrip
      173
    4. 4
      Xenon
      138
    5. 5
      +FloatingFatMan
      127
  • Tell a friend

    Love Neowin? Tell a friend!