PHP 8.2.0 RC7 available for testing

rand

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

randGenerate a random integer

Açıklama

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

Dikkat

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

min

The lowest value to return (default: 0)

max

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

Örnekler

Örnek 1 rand() example

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

echo 
rand(515);
?>

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

7771
22264
11

Notlar

Uyarı

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

up
6
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,

<?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).
up
0
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.
up
-2
Anonymous
12 years ago
Generate a random 5 character A-Z0-9  string

<?php
for ($i=0; $i<6; $i++) {
   
$d=rand(1,30)%2;
    echo
$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";'
14BW1A
up
-4
szeryf.wordpress.com
11 years ago
Much easier way to generate random string of numbers and letters:

<?php
$n
= rand(10e16, 10e20);
echo
base_convert($n, 10, 36);
?>

This generates strings of about 11 characters. Experiment with the range for rand() if you want shorter or longer.
up
-5
Anonymous
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:

<?php
srand
($argv[1]);
for (
$i = 0; $i < 10; $i++) {
    echo
rand().PHP_EOL;
}
?>

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