Xurion Posted October 16, 2006 Share Posted October 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/24074-random-numbers/ Share on other sites More sharing options...
redarrow Posted October 16, 2006 Share Posted October 16, 2006 ceil is to round up numbers and floor is to round down numbers ok.[code]<?php$rand=rand(7,2345566);echo $rand;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24074-random-numbers/#findComment-109416 Share on other sites More sharing options...
joshi_v Posted October 16, 2006 Share Posted October 16, 2006 Try this! substr(md5(uniqid(rand(), true)), 0, 12);Here 12 will be the maximum length of the random number .RegardsJoshi. Quote Link to comment https://forums.phpfreaks.com/topic/24074-random-numbers/#findComment-109419 Share on other sites More sharing options...
redarrow Posted October 16, 2006 Share Posted October 16, 2006 joshi_v that a bit stupis to md5 a refrence number any reason why cheers. Quote Link to comment https://forums.phpfreaks.com/topic/24074-random-numbers/#findComment-109420 Share on other sites More sharing options...
xsist10 Posted October 16, 2006 Share Posted October 16, 2006 [code]<?phpecho 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. Quote Link to comment https://forums.phpfreaks.com/topic/24074-random-numbers/#findComment-109426 Share on other sites More sharing options...
Xurion Posted October 16, 2006 Author Share Posted October 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/24074-random-numbers/#findComment-109444 Share on other sites More sharing options...
Daniel0 Posted October 16, 2006 Share Posted October 16, 2006 [code]<?phpfor($i=1;$i<=12;$i++){ $num .= chr(rand(48,57));}echo $num;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24074-random-numbers/#findComment-109454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.