How to generate a unique random number in php???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmdnawaz
Forum Newbie
Posts: 14
Joined: Tue Dec 21, 2010 10:55 pm

How to generate a unique random number in php???

Post by hmdnawaz »

How to generate a unique random number in php??? Any one who can help me?
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: How to generate a unique random number in php???

Post by Peter Kelly »

Well a simple way would be to use

Code: Select all

<?php
$random = rand();
?>
This will generate random numbers but i'm sure the people with the know how will tell you not to use it. But for simple use i'd just use that.
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

Re: How to generate a unique random number in php???

Post by tutigan »

You can use the rand function, although that will create a totally random number... http://php.net/manual/en/function.rand.php
to have a unique number, you can use the PHP session id, but for that to work multiple times for the same person... I don't know about that one :?
to use the session id, you can just use:

Code: Select all

session_start();
$sessionID = session_id();
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: How to generate a unique random number in php???

Post by Peter Kelly »

Thinking about it you could try using the function uniqid(); (http://uk3.php.net/manual/en/function.uniqid.php)
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: How to generate a unique random number in php???

Post by Zyxist »

What uniqueness do you want? If you want to get several numbers, and ensure the calculated random values do not repeat, you must put rand() into a loop:

Code: Select all

do
{
   $random = rand();
}
while(in_array($random, $randomValues));
$randomValues[] = $random;
Without such checking, the randomization functions must have some extra mathematical properties.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How to generate a unique random number in php???

Post by Apollo »

Instead of using the rand() function I recommend using the mt_rand() function. It produces better random (better as in longer period, less correlation between bits, etc, and it's also faster).

If you really want to make sure it's unique, combine it with a (micro-)timestamp, for example:

Code: Select all

$uniqueRandomNumber = implode('',array_map('hexdec',explode(',',wordwrap(md5(microtime(false)),8,',',true)))).mt_rand();
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to generate a unique random number in php???

Post by John Cartwright »

In a linux environment, it is always recommended to use urandom, i.e.,

Code: Select all

if ($fp = @fopen('/dev/urandom','rb') !== false) {
    $random = fread($fp,16);
    fclose($fp);
}
Otherwise, +1 for mt_rand() + seeding
Post Reply