Jump to content

Archived

This topic is now archived and is closed to further replies.

Xurion

Random Numbers

Recommended Posts

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:

[code]$mynum = ceil(rand(100000000000, 999999999999);[/code]

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.

Share this post


Link to post
Share on other sites

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

[code]
<?php
$rand=rand(7,2345566);
echo $rand;
?>
[/code]

Share this post


Link to post
Share on other sites
Try this!


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

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


Regards

Joshi.

Share this post


Link to post
Share on other sites
joshi_v that a bit stupis to md5 a refrence number any reason why cheers.

Share this post


Link to post
Share on other sites
[code]
<?php
echo getrandmax();
?>
[/code]

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.

Share this post


Link to post
Share on other sites
joshi_v's example comes back with an alphanumerical string like 58ab75d79013.

Decided to go with this:

[code]$ordernumber = ceil(rand(100000, 999999)).''.ceil(rand(100000, 999999));[/code]

Thx all

Share this post


Link to post
Share on other sites
[code]<?php
for($i=1;$i<=12;$i++)
{
$num .= chr(rand(48,57));
}

echo $num;
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.