Jump to content


Photo

randomly generated number


  • Please log in to reply
5 replies to this topic

#1 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 25 August 2006 - 04:53 PM

Hi,
Is there a way to create a randomly generated 11 digit number? I'd like to use it to create SKU numbers...

Thanks,
- Jeff

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 05:08 PM

here are a couple options:
<?php
// randomly generate and concatonate 11 digits
$sku = '';
for ($i = 0; $i < 11; $i++) {
  $num = rand(0,9);
  $sku .= $num;
}
echo $sku;


// randomly generate the entire number and pad with leading zeros
echo sprintf("%011d", rand(0,99999999999));
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Iceman512

Iceman512
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 26 August 2006 - 11:13 AM

Hi all,

I couldn't hope to match obsidian's post, but you could just try this:

<?php
$num = rand(11111111111,99999999999);
print $num;
?>

Hope it helps!
Iceman

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 26 August 2006 - 11:53 PM

However, as the max value of an integer is 2,147,483,647 (10 digits) I'd go with Obsidian's first method
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 27 August 2006 - 01:02 AM

However, as the max value of an integer is 2,147,483,647 (10 digits) I'd go with Obsidian's first method

'

If the max size of an integer is 10 digits - the max value is 2,147,483,647? Can you tell me how you calculate that? Thanks.

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 27 August 2006 - 01:32 AM

The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. [2^31-1]

Source: http://ca.php.net/intval
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users