random_int
(PHP 7)
random_int — Generates cryptographically secure pseudo-random integers
説明
random_int
( int
$min
, int $max
) : intGenerates cryptographic random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game.
この関数が使う不規則性のソースはつぎのとおりです。
- Windows では、常に » CryptGenRandom() を使います。PHP 7.2.0 以降は、常に » CNG-API を使うようになりました。
- Linux では、システムコール » getrandom(2) があればそれを使います。
- その他のプラットフォームでは、 /dev/urandom を使います。
- これらがいずれも使えない場合は、 Exception をスローします。
注意: この関数は PHP 7.0 で追加されたものですが、PHP 5.2 から 5.6 までのバージョンで使える » ユーザーランドの実装 も公開されています。
パラメータ
-
min -
The lowest value to be returned, which must be
PHP_INT_MINor higher. -
max -
The highest value to be returned, which must be less than or equal to
PHP_INT_MAX.
返り値
Returns a cryptographically secure random integer in the range
min to max, inclusive.
エラー / 例外
例
例1 random_int() example
<?php
var_dump(random_int(100, 999));
var_dump(random_int(-1000, 0));
?>
上の例の出力は、 たとえば以下のようになります。
int(248) int(-898)
参考
- random_bytes() - Generates cryptographically secure pseudo-random bytes