PHPマニュアル/PEARマニュアル | ユーザフォーラムで議論/質問 | マニュアル検索 | ハイライト | ハイライトオフ | ポータル | php spot

random_int - Generates cryptographically secure pseudo-random integers | JavaScript入門&応用&リファレンスなら「JavaScriptist」

  

random_int

(PHP 7)

random_intGenerates cryptographically secure pseudo-random integers

説明

int random_int ( int $min , int $max )

Generates 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() を使います。
  • 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_MIN or 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.

エラー / 例外

  • 適切な不規則性のソースが見つからない場合は Exception をスローします。
  • 無効なパラメータが指定された場合は TypeError をスローします。
  • If max is less than min, an Error will be thrown.

例1 random_int() example

<?php
var_dump
(random_int(100999));
var_dump(random_int(-10000));
?>

上の例の出力は、 たとえば以下のようになります。

int(248)
int(-898)

参考

  • random_bytes() - Generates cryptographically secure pseudo-random bytes

忘却曲線を使ってこの関数を確実に記憶に残す

フォーラムで「random_int - Generates cryptographically secure pseudo-random integers」について話す
各種マニュアル: PHPマニュアル | PEARマニュアル | Smarty(英語)マニュアル | PHP-GTKマニュアル | random_int - Generates cryptographically secure pseudo-random integers」をGoogle検索
copyright © 1997-2024 PHP ドキュメント作成グループ(ライセンス). provided by php spot. マニュアル: