★ wanayoo — archive 1999 http://fr.php.net/manual/en/function.rand.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<rad2deground>
view the version of this page
Last updated: Sat, 18 Sep 2004

rand

(PHP 3, PHP 4 , PHP 5)

rand -- Generate a random integer

Description

int rand ( [int min, int max])

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

Example 1. rand() example

<?php
echo rand() . "\n";
echo
rand() . "\n";

echo
rand(5, 15);
?>

The above example will output something similar to:

7771
22264
11

Note: On some platforms (such as Windows) RAND_MAX is only 32768. If you require a range larger than 32768, specifying min and max will allow you to create a range larger than RAND_MAX, or consider using mt_rand() instead.

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.

Note: 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.

See also: srand(), getrandmax(), and mt_rand().



add a note add a note User Contributed Notes
rand
edisonbattery at hotmail dot com
17-Sep-2004 08:36
uh.... DUH!!
 couldnt wait to show you know how to use mySQL---

you idiot -> everyone knows the  'order by rand()'
 is slow as hell--- it has to much to do on a very large table.

read all the posts before you add your 2 cents... or -2 cents.
onno at onnovanbraam dot com
12-Sep-2004 03: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.
aidan at php dot net
11-Sep-2004 09:45
To easily generate large numbers, or random strings such as passwords, take a look at the below function.

http://aidan.dotgeek.org/lib/?file=function.str_rand.php
a_kunyszSPAM at SUXyahoo dot com
04-Sep-2004 04:44
You won't get true random numbers from a computer but you can get some from a webcam or radioactive material.

http://www.fourmilab.ch/hotbits/ (you can fetch random bits generated from radioactive material from the web here)
http://www.lavarnd.org/ (how to set up a random number generator with a webcam)
jbarbero at u dot washington dot edu
08-Dec-2003 10:14
to jordan at do not spam .com:

The image no-cache function is simpler if you just do:
<?
$junk
= md5(time());
?>

Usage would be: <img src="image.gif?<?=$junk?>">

md5(time()) is more guaranteed to be unique, and it is faster than two md5 calls.
ewspencer at industrex dot com
03-Jun-2003 08:40
Inspired by the many useful tips posted by other users, I wrote a flexible, arbitrary, random character generator, which also produces random color values in decimal or hexadecimal format.

The procedure is encapsulated as a PHP function named randchar().

You're welcome to freely download the randchar() source code at http://www.industrex.com/opensource/randchar.phps
igge at netpond dot com
30-Oct-2002 01:35
About selecting random rows from a MySQL table:

SELECT * FROM tablename ORDER BY RAND() LIMIT 1

works for small tables, but once the tables grow larger than 300,000 records or so this will be very slow because MySQL will have to process ALL the entries from the table, order them randomly and then return the first row of the ordered result, and this sorting takes long time. Instead you can do it like this (atleast if you have an auto_increment PK):

SELECT MIN(id), MAX(id) FROM tablename;

Fetch the result into $a

$id=rand($a[0],$a[1]);

SELECT * FROM tablename WHERE id>='$d' LIMIT 1

<rad2deground>
 Last updated: Sat, 18 Sep 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Sep 18 05:21:38 2004 CEST