PHP 8.2.0 RC7 available for testing


(PHP 4, PHP 5, PHP 7, PHP 8)

randGenerate a random integer


rand(): int
rand(int $min, int $max): int

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).


Bu işlev kriptografik olarak güvenilir değerler üretmez ve kriptografik amaçlarla kullanılmamalıdır. Kriptografik olarak güvenilir bir değere ihtiyaç duyuluyorsa bunun yerine random_int() veya random_bytes() işlevi denenebilir.

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

Bilginize: As of PHP 7.1.0, rand() uses the same random number generator as mt_rand(). To preserve backwards compatibility rand() allows max to be smaller than min as opposed to returning false as mt_rand().

Bağımsız Değişkenler


The lowest value to return (default: 0)


The highest value to return (default: getrandmax())

Dönen Değerler

A pseudo random value between min (or 0) and max (or getrandmax(), inclusive).

Sürüm Bilgisi

Sürüm: Açıklama
7.2.0 rand() has received a bug fix for a modulo bias bug. This means that sequences generated with a specific seed may differ from PHP 7.1 on 64-bit machines.
7.1.0 rand() has been made an alias of mt_rand().


Örnek 1 rand() example

echo rand() . "\n";
rand() . "\n";


Yukarıdaki örnek şuna benzer bir çıktı üretir:




min max range must be within the range getrandmax(). i.e. (max - min) <= getrandmax() Otherwise, rand() may return poor-quality random numbers.

Ayrıca Bakınız

  • srand() - Seed the random number generator
  • getrandmax() - Show largest possible random value
  • mt_rand() - Generate a random value via the Mersenne Twister Random Number Generator
  • random_int() - Get a cryptographically secure, uniformly selected integer
  • random_bytes() - Get cryptographically secure random bytes

add a note

User Contributed Notes 5 notes

relsqui at armory dot com
17 years ago
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,

// instead of
// for generating 0 or 1,

// instead of
// for generating 0, 1, 2, or 3,

// instead of
// 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).
Hayley Watson
9 years ago
The Windows rand() function is quite a lot worse than merely having a low maximum value. It's an ordinary Linear Congruential Generator, which means you only need three consecutive values to be able to predict its entire future output.

Given the numbers 13050,  4267,  25352, construct the equations
4267 = (13050a+c) % 32768
25352 = (4267a+c) % 32768

Solving for a and c gives

a = 20077
c = 12345

Which means the next number that should be spat out is (25352×20077+12345) % 32768 = 19105 -- which indeed it is.

It's not the small rand_max that breaks the algorithm, it's a weakness in the  LCG algorithm itself. It's designed for when you only want a few kinda-random numbers occasionally, not if you want to generate any random-looking data.
12 years ago
Generate a random 5 character A-Z0-9  string

for ($i=0; $i<6; $i++) {
$d ? chr(rand(65,90)) : chr(rand(48,57));

# php -r 'for ($i=0; $i<6; $i++) { $d=rand(1,30)%2; echo $d ? chr(rand(65,90)) : chr(rand(48,57)); } echo "\n";'
11 years ago
Much easier way to generate random string of numbers and letters:

= rand(10e16, 10e20);
base_convert($n, 10, 36);

This generates strings of about 11 characters. Experiment with the range for rand() if you want shorter or longer.
2 years ago
Note that the algorithm change in version 7.1.0 broke the repeatability of a random sequence initialized with a given value. For example if you have a program like:

for (
$i = 0; $i < 10; $i++) {

It will will no longer produce the same results after version 7.1.0. This can be very important for some kinds of simulations. Hopefully you were using mt_rand() or something better all along, otherwise you will have some digging to do if you want your program to be able to repeat simulations from the pre-7.1.0 days... You will need to look in the PHP source archives to discover the algorithm they used to use and replicate it in your program.
To Top