SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: unique random # (php)
-
Dec 16, 2000, 01:39 #1
- Join Date
- Jul 2000
- Posts
- 69
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hi,
Can someone help me with the code for generating a unique number? It'll never repeat.
Thanks
-
Dec 16, 2000, 01:57 #2
- Join Date
- Aug 2000
- Location
- Silicon Valley
- Posts
- 2,241
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Dec 16, 2000, 11:28 #3
- Join Date
- Apr 2000
- Location
- Melbourne, Australia
- Posts
- 2,571
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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
-
Dec 16, 2000, 12:39 #4
- Join Date
- Jul 1999
- Location
- Derbyshire, UK
- Posts
- 4,411
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
Call 0800 542 9764 today and ask how we can help your business grow.
-
Dec 16, 2000, 15:05 #5
- Join Date
- Jul 2000
- Posts
- 69
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i found this code on php.net:
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.
-
Dec 16, 2000, 16:27 #6
- Join Date
- Jul 1999
- Location
- Derbyshire, UK
- Posts
- 4,411
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
Call 0800 542 9764 today and ask how we can help your business grow.
-
Dec 16, 2000, 19:59 #7
- Join Date
- Apr 2000
- Location
- Melbourne, Australia
- Posts
- 2,571
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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
-
Dec 16, 2000, 20:36 #8
- Join Date
- Aug 2000
- Location
- Land of the long white cloud
- Posts
- 556
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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!
-
Dec 16, 2000, 20:37 #9
- Join Date
- Aug 2000
- Location
- Land of the long white cloud
- Posts
- 556
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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...lolSiteOptions >> Services :: Products :: Contact
Developers of PHP, C++, Visual Basic, MySQL, and more!
-
Dec 17, 2000, 07:20 #10
- Join Date
- Jul 1999
- Location
- Derbyshire, UK
- Posts
- 4,411
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
Call 0800 542 9764 today and ask how we can help your business grow.
Bookmarks