<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.random.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.rand.php',
    1 => 'rand',
    2 => '乱数を生成する',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random 関数',
  ),
  'prev' => 
  array (
    0 => 'function.mt-srand.php',
    1 => 'mt_srand',
  ),
  'next' => 
  array (
    0 => 'function.random-bytes.php',
    1 => 'random_bytes',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/random/functions/rand.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.rand" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">rand</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">rand</span> &mdash; <span class="dc-title">乱数を生成する</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.rand-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>rand</strong></span>(): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <div class="methodsynopsis dc-description"><span class="methodname"><strong>rand</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$min</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$max</code></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="simpara">
   オプションの引数
   <code class="parameter">min</code>,<code class="parameter">max</code>
   を省略してコールした場合、<span class="function"><strong>rand()</strong></span> は 0 と
   <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span> の間の擬似乱数(整数)を返します。
   例えば、5 から 15 まで（両端を含む）の乱数を得たい場合、
   <code class="literal">rand(5, 15)</code> とします。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="para">
  この関数が生成する値は、暗号学的にセキュアではありません。そのため、これを暗号や、戻り値を推測できないことが必須の値として使っては <em>いけません</em>。
 </p>
 <p class="para">
  暗号学的にセキュアな乱数が必要な場合は、<span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> を <span class="classname"><a href="class.random-engine-secure.php" class="classname">Random\Engine\Secure</a></span> と一緒に使いましょう。簡単なユースケースの場合、<span class="function"><a href="function.random-int.php" class="function">random_int()</a></span> と <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span> 関数が、オペレーティングシステムの <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> を使った、 便利で安全な <abbr title="Application Programming Interface">API</abbr> を提供します。
 </p>
</div>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="simpara">
    この関数は、乱数のソースとしてグローバルな Mt19937 (メルセンヌ・ツイスター) インスタンスを使用するため、グローバルな Mt19937 を使用する他のすべての関数と状態を共有します。
    これらの関数のいずれかを使用すると、スコープに関係なく、<em>すべての</em>他の関数のシーケンスも進みます。
  </p>
  <p class="simpara">
    <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> または <span class="function"><a href="function.srand.php" class="function">srand()</a></span> に既知の値を指定してシードすることで再現可能なシーケンスを生成する場合、この関数からの出力も再現可能になります。
  </p>
  <p class="simpara">
    新しく書くコードでは、すべて <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> のメソッドを使用することを推奨します。
 </p>
</div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    PHP 7.1.0 より前のバージョンでは、（Windows のような）いくつかのプラットフォームにおいて
    <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span> は 32767 と小さな値となっていました。
    32767 より広い範囲にしたい場合、
    <code class="parameter">min</code> および <code class="parameter">max</code> を指定することで、
    これより大きな範囲の乱数を生成することができます。
    もしくは、
    <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> をかわりに使用してみてください。
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    PHP 7.1.0 以降、<span class="function"><strong>rand()</strong></span> は、
    <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> と同じ乱数生成器を使います。
    下位互換性を保持するために、<span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> は <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返すのと対照的に、<span class="function"><strong>rand()</strong></span> は
    <code class="parameter">max</code> が <code class="parameter">min</code>
    よりも小さいことを許します。
   </span>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.rand-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">min</code></dt>
     <dd>
      <p class="para">
       返す値の最小値 (デフォルトは 0)。
      </p>
     </dd>
    
    
     <dt><code class="parameter">max</code></dt>
     <dd>
      <p class="para">
       返す値の最大値 (デフォルトは <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>)。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.rand-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   <code class="parameter">min</code> (あるいは 0)
   から <code class="parameter">max</code> (あるいは <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>、それぞれ端点を含む)
   までの間の疑似乱数値を返します。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.rand-changelog">
  <h3 class="title">変更履歴</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>バージョン</th>
       <th>説明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><strong>rand()</strong></span> 関数のモジュロバイアスに関するバグが
        <a href="migration72.incompatible.php#migration72.incompatible.rand-mt_rand-output" class="link">修正されました</a>。
        これは、特定のシードから生成されるシーケンスが 64bit PHP 7.1 のそれとは異なる可能性があるということです。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><strong>rand()</strong></span> は、<span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> の
        <a href="migration71.incompatible.php#migration71.incompatible.rand-srand-aliases" class="link">エイリアスになりました。</a>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.rand-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 <span class="function"><strong>rand()</strong></span> の例</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">rand</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">rand</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">15</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">7771
22264
11</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.rand-notes">
  <h3 class="title">注意</h3>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    <code class="parameter">min</code> から <code class="parameter">max</code>
    までの幅を <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span> の範囲内におさめる必要があります。
    つまり、abs(<code class="parameter">max</code> - <code class="parameter">min</code>) &lt;=
    <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span> でなければいけないということです。
    この範囲をこえてしまうと、
    <span class="function"><strong>rand()</strong></span> が返す値のランダム性が、
    低品質になってしまいます。
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.rand-seealso">
  <h3 class="title">参考</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.srand.php" class="function" rel="rdfs-seeAlso">srand()</a> - 乱数生成器を初期化する</span></li>
    <li><span class="function"><a href="function.getrandmax.php" class="function" rel="rdfs-seeAlso">getrandmax()</a> - 乱数の最大値を取得する</span></li>
    <li><span class="function"><a href="function.mt-rand.php" class="function" rel="rdfs-seeAlso">mt_rand()</a> - メルセンヌ・ツイスター乱数生成器を介して乱数値を生成する</span></li>
    <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - 暗号学的にセキュアな方法で、等確率に出る整数を取得する</span></li>
    <li><span class="function"><a href="function.random-bytes.php" class="function" rel="rdfs-seeAlso">random_bytes()</a> - 暗号学的にセキュアな、ランダムなバイト列を生成する</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>