★ wanayoo — archive 1999 http://www.php.net/manual/hu/function.rand.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Matematika
Quick Reference
English version of this pageGerman version of this pageJapanese version of this pageItalian version of this pageFrench version of this page
Matematika
* abs
* acos
* asin
* atan
* atan2
* base_convert
* bindec
* ceil
* cos
* decbin
* dechex
* decoct
* deg2rad
* exp
* floor
* getrandmax
* hexdec
* log
* log10
* max
* min
* mt_rand
* mt_srand
* mt_getrandmax
* number_format
* octdec
* pi
* pow
* rad2deg
* rand
* round
* sin
* sqrt
* srand
* tan
Manual: rand
View the source code for this pageSearch the site



Previous page
 rad2deg
 Updated
Sat, 12 Aug 2000
round 
Next page


rand

(PHP3 , PHP4 )

rand -- Generate a random value

Description

int rand ([int min [, int max]])

If called without the optional min, max arguments rand() returns a pseudo-random value between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).

Remember to seed the random number generator before use with srand().

Megjegyzés: 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 und 15.

See also srand(), getrandmax(), mt_rand(), mt_srand(), and mt_getrandmax().


User Contributed Notes: rand


j@avias.com
03-Sep-1998 07:46
>Returns a pseudo-random value between
>0 and RAND_MAX. If you want a random
>number between 5 and 15, for example,
>use rand()%10 + 5.
May be between 5 and 14?

PS thks for php!



zkimmel@earthlink.net
10-May-1999 04:43
On win32, at least, the second input to rand() seems to be EXCLUSIVE, not inclusive.

rand(0,2) => gives a number from 0 to 1
rand(0,3) => gives a number from 0 to 2



m.stolte@samhoud.nl
16-Dec-1999 07:57
srand doesn't seem to be needed, since using only rand(1,5); returns random values too....

Maarten Stolte



cybin@whid.net
24-Dec-1999 03:42
srand() is needed... at least on my apache server. without seeding the random generator, it always chose 1. a good seed to use (from the srand() page)

srand((double)microtime()*1000000);



xssiv@NOSPAM.theglobe.com.SPAMNO
28-Jan-2000 01:20
Seems that both min and max values are needed (3.0.14). So:
<PRE>
&lt;?
srand((double)microtime()*1000000);
echo "rand(1,99)";
?>
</PRE>



joestump98@yahoo.com
02-Feb-2000 01:10
Seeding the random number is definately required. If you want to pull a random record from a MySQL database try the following it worked for me on www.miester.org for my quotes database.

&lt;?php
//remember to connect to you DB as usual
$sql = "SELECT * FROM table";
$result = mysql_query($sql);

//return number of records
$nums = mysql_num_rows($result);

//seed the random number
srand((double)microtime()*1000000);
$random = rand(5,15);

//now select the random record
$sql = "SELECT * FROM table WHERE id='$random'";
$result = mysql_query($sql);

//bring random record into $myrow array
$myrow = mysql_fetch_array($result);

//now just echo it using $myrow[] pattern

?>



matthewjetthall@newhmw.com
07-Mar-2000 02:04
Here's a bit of code I use in an include file to generate a random record from a mySQL database that has an ID field and one other field. Contact me with questions.

&lt;?php
// =-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=
//This code should be included in a file needing a random number function
//Include file for Random Number and SQL Row Generator
//Matthew Jett Hall -- www.newhmw.com -- Tuesday, 7, March, 2000 at 01:04:04
//matthewjetthall@newhmw.com / www.tastylinux.com
// =-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=
// $long_seed = the seed for the random number
// $str_tableName = the name of the table upon which to perform the random generation
// =-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=
function randomNumber($long_seed) {
// seed with microseconds since last "whole" second
srand((double)microtime()*1000000);
$randval = rand(1,$long_seed);
return($randval);
}
function randomItem($str_tableName) {
//get a random item from the table based upon the table count
require('db.inc'); //connect to the database
//get the number of records in the database
$str_SQL='select * from ' . $str_tableName;
$result=mysql_query($str_SQL); //check the # of records
$int_numberOfRows = @mysql_num_rows($result); //count how many records there are
//select a single record based upon a random # generated from the row count
$str_SQL='select * from ' . $str_tableName . ' where ID='. randomNumber($int_numberOfRows);
$result=mysql_query($str_SQL);
while ($myrow=mysql_fetch_row($result)){
$hold=$myrow[1];
}
return($hold);
}
?>



tomber@vinp.de
12-Apr-2000 06:07
What about the following for a 10 Digit Number: rand(1000000000,9999999999); If you use this way you actually never have any 0 as the first number, but you always have a 10digit number.


online-caesar@geocities.com
13-Jul-2000 06:05
I am using PHP4 and it seems that rand and srand are not supported in classes.


ominousone@home.com
24-Jul-2000 04:00
It appears 4.0.1+ has a problem with rand()s when supplied with arguments above 9999. For instance:

$foo = rand(1,9999); # fine
$foo = rand(1,99999); # error



whoisthisfreak@bigfoot.com
12-Aug-2000 02:39
I have found a better and more reliable way of getting a random record from an sql database with php. Methods that I had found on the Internet forced you to linearly cycle through records until you reached a suitably random one. Other methods required you to have a consecutively numbered index field in the table. Although most tables DO have numerical indexes, sometimes the numbers are not consecutive. This is what I came up with:

function randomrec($table, &$row, $db)
{

$sql = "SELECT * FROM $table";
$result = mysql_query($sql, $db);
$numrecs = mysql_num_rows($result);
srand((double)microtime()*1000000);
$recnum = rand(0, $numrecs-1);
$sql = "SELECT * FROM $table LIMIT $recnum, 1";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);

}



 About Notes


Previous page
 rad2deg
 Updated
Sat, 12 Aug 2000
round 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.