Jump to content


Photo

Random Numbers


  • Please log in to reply
6 replies to this topic

#1 Xurion

Xurion
  • Members
  • PipPipPip
  • Advanced Member
  • 259 posts

Posted 16 October 2006 - 10:09 AM

Hi all,

I haven't played around with random numbers in PHP much before and I am trying to create a random number that has 12 digits (an order number for a client)

At the moment i have something like this:

$mynum = ceil(rand(100000000000, 999999999999);

Thinking that this should generate a number from 100000000000 - 999999999999 it doesn't. It sometimes comes up with negative numbers like -5786142...?

Am I using this code in an incorrect way?

Thanks in advance.

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 October 2006 - 10:15 AM


ceil is to round up numbers and floor is to round down numbers ok.

<?php
$rand=rand(7,2345566);
echo $rand;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 joshi_v

joshi_v
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 16 October 2006 - 10:23 AM

Try this!


substr(md5(uniqid(rand(), true)), 0, 12);

Here 12 will be the maximum length of the random number .


Regards

Joshi.

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 October 2006 - 10:24 AM

joshi_v that a bit stupis to md5 a refrence number any reason why cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 16 October 2006 - 10:34 AM

<?php
echo getrandmax();
?>

Random numbers only go up to 2147483647.
You're working with integers remember. Once it passes 2147483647 it will become -2147483648 and then -2147483647 and cycle back to 0 again.
SA PHP Archives - www.phparchives.za.org

#6 Xurion

Xurion
  • Members
  • PipPipPip
  • Advanced Member
  • 259 posts

Posted 16 October 2006 - 11:17 AM

joshi_v's example comes back with an alphanumerical string like 58ab75d79013.

Decided to go with this:

$ordernumber = ceil(rand(100000, 999999)).''.ceil(rand(100000, 999999));

Thx all

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 11:56 AM

<?php
for($i=1;$i<=12;$i++)
{
	$num .= chr(rand(48,57));
}

echo $num;
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users