Heres another function to generate Random Unique numbers.
<?php
class UniqueRand{
var $alreadyExists = array();
function uRand($min = NULL, $max = NULL){
$break='false';
while($break=='false'){
$rand=mt_rand($min,$max);
if(array_search($rand,$this->alreadyExists)===false){
$this->alreadyExists[]=$rand;
$break='stop';
}else{
echo " $rand already! ";
print_r($this->alreadyExists);
}
}
return $rand;
}
}
$rand=new UniqueRand();
echo"<b> ". $rand->uRand(0,5)."</b>";
echo"<b> ". $rand->uRand(5,5)."</b>";
echo"<b> ". $rand->uRand(10,5)."</b>";
?>
rand
(PHP 4, PHP 5)
rand — Generate a random integer
Descrierea
If called without the optional min , max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 5 and 15 (inclusive), for example, use rand(5, 15).
Notă: On some platforms (such as Windows), getrandmax() is only 32768. If you require a range larger than 32768, specifying min and max will allow you to create a range larger than this, or consider using mt_rand() instead.
Notă: Începând cu PHP 4.2.0 nu este nevoie de a însămânţa generatorul de numere aleatoare prin intermediul srand() sau mt_srand(), deoarece aceasta se efectuează în mod automat.
Parametri
- min
-
The lowest value to return (default: 0)
- max
-
The highest value to return (default: getrandmax())
Valorile întroarse
A pseudo random value between min (or 0) and max (or getrandmax(), inclusive).
Istoria schimbărilor
| Versiunea | Descriere |
|---|---|
| Since 3.0.7 | In versions before 3.0.7 the meaning of max was range . To get the same results in these versions the short example should be rand (5, 11) to get a random number between 5 and 15. |
Exemple
Example #1 rand() example
<?php
echo rand() . "\n";
echo rand() . "\n";
echo rand(5, 15);
?>
Exemplul de mai sus va afişa ceva similar cu:
7771 22264 11
rand
14-Aug-2008 06:55
07-Aug-2008 02:52
thomas at tgohome dot com
An article for you:
“Do not use ORDER BY RAND()” or “How to get random rows from table?”
http://tinyurl.com/do-not-use-order-by-rand-or-ho
31-Jul-2008 08:46
You don't need fancy queries to grab a random row(s) from MySQL; it's actually quite simple. In fact, you can do it in a single query.
<?php
$random_rows = array();
$query = mysql_query("SELECT * FROM tablename ORDER BY RAND() LIMIT 1");
while($row = mysql_fetch_array($query))
{
$random_rows[] = $row;
}
?>
You can extend LIMIT to any number, so you can select 10 random rows. Also, because it uses ORDER BY the rows are always unique if you select 10 rows, for example.
This might work on other database systems such as MsSQL and PostgreSQL but I haven't tested them.
30-Jul-2008 04:29
quick way to generate randomish numbers and simple strings.
no messing around with functions, so you can just pop the line into the middle of your existing code.
not the most perfect for sure, but ok for plenty of situations...
<?php
$random_number = intval( "0" . rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9) ); // random(ish) 5 digit int
$random_string = chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)); // random(ish) 5 character string
?>
hope someone finds it useful for somthing.
regards,
deeeeeen alxndr0u
09-Jul-2008 06:08
Okay, here is the final version of a simple function to generate random (but unique) numbers. As I said earlier, it will use less memory when an explicit range is supplied, (the smaller the better). You could always customize it, e.g. the hash algorithm, the return value once it has exhausted the values within a supplied range, etc.
function urand($min = NULL, $max = NULL){
static $alreadyGenerated = array();
$range = ($min && $max) ? ($max - $min) + 1 : NULL;
do{
$randValue = ($range) ? rand($min, $max) : rand();
$key = md5($randValue);
if(count($rangeList) == $range) return NULL;
if($range) $rangeList[$key] = $randValue;
}while($alreadyGenerated[$key]);
unset($rangeList);
$alreadyGenerated[$key] = $randValue;
return $randValue;
}
09-Jul-2008 02:45
A follow up on the simple class to generate a random (but unique) number. It obviously uses less memory when you specify a small range.
class UniquelyRandom{
private static $alreadyGenerated = array();
public function urand($min = NULL, $max = NULL)
{
$range = ($min) && ($max) ? ($max - $min) + 1 : NULL;
do{
$randValue = ($range) ? rand($min, $max) : rand();
$key = md5($randValue);
if(count($rangeList) == $range) return NULL;
if($range) $rangeList[$key] = NULL;
}while(self::$alreadyGenerated[$key]);
unset($rangeList);
self::$alreadyGenerated[$key] = $randValue;
return $randValue;
}
}
29-Jun-2008 08:29
This one is better:
function random_letters ($numofletters=10) {
$v="";
for($i=0;$i<$numofletters;$i++) $v.=chr(rand(65,90));
return $v;
}
29-May-2008 04:36
For some people who would need to generate random string:
<?php
function random_letters ($numofletters) {
if (!isset($numofletters)) $numofletters = 10; // if $numofletters is not specified sets to 10 letters
$literki = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'W');
$ilosc_literek = count($literki);
for ($licz = 0; $licz < $numofletters; $licz++) {
$rand = rand(0, $ilosc_literek-1);
$vercode = $vercode.$literki[$rand];
}
}
?>
26-Apr-2008 12:27
emad_ramahi at hotmail dot com:
I've actually noticed that with a large dataset (100k rows), the query dramatically slows down the server and performance is way too bad.
The way I see it, you have to workable solutions:
Using PHP:
<?php
//$Table holds the name of the table we're getting the random row from
//$Rows specifies how many rows we need to fetch
function mysql_rand ($Query, $Rows = 1) {
//getting the table name from $query
//what you can do, is replace the $query argument with $table,
//this way to dont have to search for the table's name
$SQL = sprintf ('SELECT COUNT(*) FROM %s', substr ($query, stripos ($query, 'from')+5, strpos ($query, ' ')));
$Max = mysql_result (mysql_query ($SQL), 0);
$Random = rand (0, $Max);
//If the random number is 99, and the database only has 100 rows
//We'll subtract the random number, so we don't exceed 100, thus preventing a MySQL error message
return $Query . sprintf ('LIMIT %d, %d', ($Max < $Rows) ? 0 : (($Random > $Max - $Rows) ? $Max - $Rows : $Random), $Rows);
}
//Instead of using MySQL's RAND(), we use LIMIT to fetch rows
//E.g. LIMIT 5,9 fetches from row 5, and the subsequent 9
$SQL = mysql_rand ('SELECT row FROM table'); //SELECT row FROM table LIMIT x, y
?>
or SQL:
SELECT * FROM Table T JOIN (SELECT FLOOR(MAX(ID)*RAND()) AS ID FROM Table) AS x ON T.ID >= x.ID LIMIT 1;
09-Mar-2008 01:16
Hi All,
For those whom wants to get a random value from MySQL:
select coumnName from TableName order by rand()
25-Feb-2008 12:37
Using the script below with rand() instead of mt_rand(),
I was suprised to find many "words" and part of "phrases" in common.
mt_rand() is highly recomanded when you want to build test cases.
$ressbdd = mysql_connect("localhost", "usr", "pass")
or die("Could not connect: " . mysql_error());
for($i=0;$i<100000;$i++){
$xx1=mt_rand(15,80);
$texte = phrase($xx1);
$sql = 'INSERT into phrases.valeurs set valeur = \'' . $texte . '\'';
mysql_query( $sql , $ressbdd );
}
//=============================================
function phrase($n){
$phrase='';
for($i=0;$i<$n;$i++){
$xx2=mt_rand(2,20);
$phrase.=mot($xx2) . ' ';
}
return(substr($phrase,0,-1).'.'); // phrase ends with a .
}
//=============================================
function mot($n){
if($n<=0) return 'hello';
if($n>=31) return 'world';
$voyelle =Array('a','e','i','o','u','y');
$consonne=Array(
'b','c','d','f','g','h','j','k','l','m',
'n','p','q','r','s','t','v','w','x','z');
$mot='';
for($i=0;$i<$n;$i++){
if($i%2==0){
$xx3=mt_rand(0,19);
$mot.=$consonne[$xx3];
}else{
$xx4=mt_rand(0,5);
$mot.=$voyelle[$xx4];
}
}
return $mot;
}
06-Nov-2007 01:24
Note, the function used by PHP to constrain a random number between (min, max) is the following:
$number = $min + (($max - $min + 1) * ($number / ($rand_max + 1));
where the following:
$number - the initially generated random number
$min - the minimum in the range
$max - the maximum in the range
$rand_max - the maximum possible random value
What this algorithm does is constrain the generated number to a 0-1 range, then multiply it against your range, mapping the two to each other.
In practice you'll see the following results:
For a generated $number of 16384 and a $rand_max of 32768:
rand(0, 10) = 5
rand(0, 100) = 50
rand(0, 1000) = 500
with the additional property that if the range you're asking for is larger than $rand_max, random numbers will be in a multiple of $max/$rand_max.
10-Sep-2007 06:18
A small comment on phpdev-dunnbypauls conclusion that rand() only generates numbers that are a multiply of 3.
<?php
$n = rand(0,100000); // with MAX_RAND=32768
?>
Since, 100000/32768=3.05 you get multiples of 3. The random integer will be multiplied by 3.05 to fit between 0 and 100000. rand() works fine, if you don't ask for bigger numbers then RAND_MAX.
16-Jun-2007 12:43
rand function returns just a whole numbers. If you want a random float, then here's an elegant way:
<?php
function random_float ($min,$max) {
return ($min+lcg_value()*(abs($max-$min)));
}
?>
23-May-2007 05:36
Note that the automatic seeding seems to be done with the current number of seconds which means you can get the same results for several runs on a fast server. Either call srand() yourself with a more frequently changing seed or use mt_rand() which doesn't appear to suffer from the problem.
05-Apr-2007 03:42
isn't this just a simpler way of making a random id for somthing? I mean i know that there is a very slight chance that a duplicate could be made but its a very, very, very small chance, nearly impossible.
$rand = mt_rand(0, 32);
$code = md5($rand . time());
echo "$code";
and if you don't want it the md5 can be removed, I've just added it as a prefer it there :)
Jon
07-Mar-2007 08:51
Here's an interesting note about the inferiority of the rand() function. Try, for example, the following code...
<?php
$r = array(0,0,0,0,0,0,0,0,0,0,0);
for ($i=0;$i<1000000;$i++) {
$n = rand(0,100000);
if ($n<=10) {
$r[$n]++;
}
}
print_r($r);
?>
which produces something similar to the following output (on my windows box, where RAND_MAX is 32768):
Array
(
[0] => 31
[1] => 0
[2] => 0
[3] => 31
[4] => 0
[5] => 0
[6] => 30
[7] => 0
[8] => 0
[9] => 31
[10] => 0
)
Within this range only multiples of 3 are being selected. Also note that values that are filled are always 30 or 31 (no other values! really!)
Now replace rand() with mt_rand() and see the difference...
Array
(
[0] => 8
[1] => 8
[2] => 14
[3] => 16
[4] => 9
[5] => 11
[6] => 8
[7] => 9
[8] => 7
[9] => 7
[10] => 9
)
Much more randomly distributed!
Conclusion: mt_rand() is not just faster, it is a far superior algorithm.
02-Mar-2007 06:51
Using rand()%x is faster than rand(0,x) yes, but it is wrong.
Consider the following example:
RAND_MAX is 32768 (like on Windows for example)
You use rand()%30000
Imagine rand() returns a value between 30000 and 32768.
Modulo could make any value between 0 and 2768, but not any between 2769 and 29999 (except the value is below 29999).
This would double the chance of getting a number between 0 and 2768, which is speaking against the principles of randomness.
27-Sep-2006 05:42
frank, nick at nerdynick dot com, and kniht
this is now O(n) instead of O(n^2) ish...
<?php
function rand_permute($size, $min, $max)
{
$retval = array();
//initialize an array of integers from $min to $max
for($i = $min;$i <= $max;$i++)
{
$retval[$i] = $i;
}
//start with the the first index ($min).
//randomly swap this number with any other number in the array.
//this way we guarantee all numbers are permuted in the array,
//and we assure no number is used more than once (technically reiterating prev line).
//therefore we don't have to do the random checking each time we put something into the array.
for($i=$min; $i < $size; $i++)
{
$tmp = $retval[$i];
$retval[$i] = $retval[$tmpkey = rand($min, $max)];
$retval[$tmpkey] = $tmp;
}
return array_slice($retval, 0, $size);
}
?>
13-Jun-2005 05:32
Random numbers with Gauss distribution (normal distribution).
A correct alghoritm. Without aproximations, like Smaaps'
It is specially usefull for simulations in physics.
Check yourself, and have a fun.
<?php
function gauss()
{ // N(0,1)
// returns random number with normal distribution:
// mean=0
// std dev=1
// auxilary vars
$x=random_0_1();
$y=random_0_1();
// two independent variables with normal distribution N(0,1)
$u=sqrt(-2*log($x))*cos(2*pi()*$y);
$v=sqrt(-2*log($x))*sin(2*pi()*$y);
// i will return only one, couse only one needed
return $u;
}
function gauss_ms($m=0.0,$s=1.0)
{ // N(m,s)
// returns random number with normal distribution:
// mean=m
// std dev=s
return gauss()*$s+$m;
}
function random_0_1()
{ // auxiliary function
// returns random number with flat distribution from 0 to 1
return (float)rand()/(float)getrandmax();
}
?>
JanS
student of astronomy
on Warsaw University
06-Jun-2005 10:44
Lately I needed some random numbers with a gaussian (normal) distribution, not evenly distributed as the numbers generated by rand(). After googling a while, I found out that there is no perfect algrorithm that creates such numbers out of evenly distruted random numbers but a few methods that have similar effect. The following function implements all three algorithms I found- The the last two methods create numbers where you can find a lower and upper boundary and the first one will create a number from time to time (such as one in every 10000) that may be very far from the average value. Have fun testing and using it.
function gauss($algorithm = "polar") {
$randmax = 9999;
switch($algorithm) {
//polar-methode by marsaglia
case "polar":
$v = 2;
while ($v > 1) {
$u1 = rand(0, $randmax) / $randmax;
$u2 = rand(0, $randmax) / $randmax;
$v = (2 * $u1 - 1) * (2 * $u1 - 1) + (2 * $u2 - 1) * (2 * $u2 - 1);
}
return (2* $u1 - 1) * (( -2 * log($v) / $v) ^ 0.5);
// box-muller-method
case "boxmuller":
do {
$u1 = rand(0, $randmax) / $randmax;
$u2 = rand(0, $randmax) / $randmax;
$x = sqrt(-2 * log($u1)) * cos(2 * pi() * $u2);
} while (strval($x) == "1.#INF" or strval($x) == "-1.#INF");
// the check has to be done cause sometimes (1:10000)
// values such as "1.#INF" occur and i dont know why
return $x;
// twelve random numbers
case "zwoelfer":
$sum = 0;
for ($i = 0; $i < 12; $i++) {
$sum += rand(0, $randmax) / $randmax;
}
return $sum;
}
}
21-Jan-2005 02:23
Don't forget, it's faster to use bitwise operations when you need a random number that's less than some power of two. For example,
<?php
rand()&1;
// instead of
rand(0,1);
// for generating 0 or 1,
rand()&3;
// instead of
rand(0,3);
// for generating 0, 1, 2, or 3,
rand()&7;
// instead of
rand(0,7)
// for generating 0, 1, 2, 3, 4, 5, 6, or 7,
?>
and so on. All you're doing there is generating a default random number (so PHP doesn't have to parse any arguments) and chopping off the piece that's useful to you (using a bitwise operation which is faster than even basic math).
12-Sep-2004 06:58
>>Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.<<
Do realize that you don't HAVE to call srand, but if you don't you get the same results every time... Took me quite a while to get to that conclusion, since I couldn't figure it out:
foreach($Unit AS $index => $value) {
srand() ;
$rand_pos_x = rand(0, $EVAC['x_max']) ;
$rand_pos_y = rand(0, $EVAC['y_max']) ;
}
works, but:
foreach($Unit AS $index => $value) {
$rand_pos_x = rand(0, $EVAC['x_max']) ;
$rand_pos_y = rand(0, $EVAC['y_max']) ;
}
doesnt even though they say it should.
I'm using Windows XP Pro (Windows NT EVOLUTION 5.1 build 2600), PHP Version 4.3.3, CGI/FastCGI.
