hi,
Can someone help me with the code for generating a unique number? It'll never repeat.
Thanks
| SitePoint Sponsor |
hi,
Can someone help me with the code for generating a unique number? It'll never repeat.
Thanks





This is a code fragement that should give you random number, but it can't guarantee uniqueness because it's random (or randomish)!
mt_srand ((double) microtime() * 1000000);
$randval = mt_rand(min,max);
You suppy min/max for mt_rand()
If you want something that never repeat, that means it doesn't even have a boundary (infinite numbers) and all variable types in PHP (and most other languages) are finite.
- Son Nguyen
AdSpeed.com - Ad Serving and Ad Management Made Easy
vinasite,
Maybe if you told us what you wanted the number for, we could tell you a better way to do it!
Kevin Yank
CTO, sitepoint.com
I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
Baby’s got back—a hard back, that is: The Ultimate CSS Reference
If you want a random number and aren't doing many requests per second you can try:
$rnd = time().microtime() as a solution.
If you are after a random number for a seesion key then I suggest that you use something like the following:
srand( (double) microtime() * 1000000 );
$key = md5(uniqid( getmypid().rand() ) );
The getmypid() is in there as uniquid can generate the same id sometimes on SMP machines and the getmypid() stops this from being a posibility as the same process can't be running on more than one CPU at the same time.
Karl Austin :: Profile :: KDA Web Services Ltd.
Business Web Hosting :: Managed Dedicated Hosting :: From £250/m
Personal Web Hosting :: Budget Web Hosting :: From £50/y
Call 0800 542 9764 today and ask about our Cloud Hosting Platform
i found this code on php.net:
i tried it for a couple hundreds of time and it generates something unique everytime. Im just wondering when it'll repeat itself.Code:function getUnique ($order_num_len = 10) { $nps = ""; mt_srand ((double) microtime() * 1000000); while (strlen($nps)<$order_num_len) { $c = chr(mt_rand (0,255)); if (eregi("^[A-Z0-9]$", $c)) $nps = $nps . strtolower($c); } return ($nps); }
This code im looking for will be use to generate an ID for an ecard site i'm working on. The id will be used to pick up the card.
Thanks
Sang N.
Ahh, well the code I posted will do the job just fine for you as it is designed to be used as a session key.
Karl Austin :: Profile :: KDA Web Services Ltd.
Business Web Hosting :: Managed Dedicated Hosting :: From £250/m
Personal Web Hosting :: Budget Web Hosting :: From £50/y
Call 0800 542 9764 today and ask about our Cloud Hosting Platform
vinasite,
That code still uses a random number generator, so you have no guarantee that a unique number will be created every time.
It's like rolling a die. Chances are three rolls will produce three different numbers between 1 and 6, but you might also get two of the same number, or even all three the same. Think of your code above as a die with many, many sides.
Kevin Yank
CTO, sitepoint.com
I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
Baby’s got back—a hard back, that is: The Ultimate CSS Reference



Here is my "unique" (no pun intended) solution to this.
Why not.
grab the unix timestamp
concantante it with the users ip address (remove the ".")
use this to seed the random number generator
$seed = time();
$ip = str_replace(".", "", $REMOTE_ADDR);
$seed.=$ip;
srand($seed);
$randomkey = rand();
Of course you could just use the ip with the uniz timestamp but that still has some degree of repetition.
The above code will generate a different number 99.9 reaccurring % of times.
If it fails your site is not meant to succeed anyway...very badluck, or a pack of lucky hackers!.
SiteOptions >> Services :: Products :: Contact
Developers of PHP, C++, Visual Basic, MySQL, and more!



But of course seeing as it is a greeting card site it will probably use a db.
In which case why would you not just use a unique key with the db, i have never seen MySQL screw that up...lol
SiteOptions >> Services :: Products :: Contact
Developers of PHP, C++, Visual Basic, MySQL, and more!
That's a pretty valid point and one that is guaranteed to be unique in the database.
You can use mysql_insert_id() to get the ID of the last inserted item from the database so you could make up a url for the user to visit to get their card.
Karl Austin :: Profile :: KDA Web Services Ltd.
Business Web Hosting :: Managed Dedicated Hosting :: From £250/m
Personal Web Hosting :: Budget Web Hosting :: From £50/y
Call 0800 542 9764 today and ask about our Cloud Hosting Platform
Bookmarks