<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.mt-rand.php',
    1 => 'mt_rand',
    2 => 'Erzeugt eine Zufallszahl nach dem Mersenne-Twister-Verfahren',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random Funktionen',
  ),
  'prev' => 
  array (
    0 => 'function.mt-getrandmax.php',
    1 => 'mt_getrandmax',
  ),
  'next' => 
  array (
    0 => 'function.mt-srand.php',
    1 => 'mt_srand',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'reference/random/functions/mt-rand.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.mt-rand" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mt_rand</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mt_rand</span> &mdash; <span class="dc-title">Erzeugt eine Zufallszahl nach dem Mersenne-Twister-Verfahren</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mt-rand-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mt_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>mt_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">
   Viele Zufallszahlengeneratoren, die auf älteren libc-Versionen basieren,
   haben seltsame oder doch zumindest unerwartete Verhaltensweisen und sind
   zudem recht langsam. Die Funktion <span class="function"><strong>mt_rand()</strong></span> ist ein
   vollwertiger Ersatz für das ältere <span class="function"><a href="function.rand.php" class="function">rand()</a></span>. Sie verwendet
   einen Zufallszahlengenerator mit den bekannten Charakteristika des
   <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">&raquo;&nbsp;Mersenne Twisters</a>, der Zufallszahlen
   viermal schneller generiert als der durchschnittliche libc-rand()-Aufruf.
  </p>
  <p class="simpara">
   Wenn die Funktion ohne Angabe von <code class="parameter">min</code> und/oder
   <code class="parameter">max</code> aufgerufen wird, gibt
   <span class="function"><strong>mt_rand()</strong></span> eine Pseudozufallszahl zwischen 0 und
   <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span> zurück. Wenn zum Beispiel eine
   Zufallszahl zwischen 5 und 15 (inklusive) benötigt wird, muss
   <code class="literal">mt_rand(5, 15)</code> verwendet werden.
  </p>
  <div class="caution"><strong class="caution">Achtung</strong>
 <p class="para">
  Diese Funktion erzeugt keine kryptografisch sicheren Werte und
  <em>darf nicht</em> für kryptografische Zwecke verwendet werden
  oder für Zwecke, bei denen die zurückgegebenen Werte nicht abschätzbar sein
  dürfen.
 </p>
 <p class="para">
  Falls kryptographisch sichere Zufallszahlen benötigt werden, kann der
  <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> mit der
  <span class="classname"><a href="class.random-engine-secure.php" class="classname">Random\Engine\Secure</a></span>-Engine verwendet werden. Für
  einfache Anwendungsfälle bieten die Funktionen <span class="function"><a href="function.random-int.php" class="function">random_int()</a></span>
  und <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span> eine bequeme und sichere
  <abbr title="Application Programming Interface">API</abbr>, die den <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> des
  Betriebssystems verwendet.
 </p>
</div>
  <div class="caution"><strong class="caution">Achtung</strong>
 <p class="simpara">
    This function uses the global Mt19937 (“Mersenne Twister”) instance as the source of randomness and thus shares its state with all other functions using the global Mt19937.
    Using any of these functions advances the sequence for <em>all</em> the other functions, regardless of scope.
  </p>
  <p class="simpara">
    Generating repeatable sequences by seeding <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> or <span class="function"><a href="function.srand.php" class="function">srand()</a></span> with a known value will also yield repeatable output from this function.
  </p>
  <p class="simpara">
    Prefer using <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> methods in all newly written code.
 </p>
</div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mt-rand-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">min</code></dt>
     <dd>
      <p class="para">
       Der optionale niedrigste Wert, der zurückgegeben werden kann
       (Standardwert: 0)
      </p>
     </dd>
    
    
     <dt><code class="parameter">max</code></dt>
     <dd>
      <p class="para">
       Der optionale höchste Wert, der zurückgegeben werden kann
       (Standardwert: <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>)
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mt-rand-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Ein zufälliger Integerwert zwischen <code class="parameter">min</code> (oder 0) und
   <code class="parameter">max</code> (oder <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>,
   inklusive).
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.mt-rand-errors">
  <h3 class="title">Fehler/Exceptions</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Wenn <code class="parameter">max</code> kleiner ist als <code class="parameter">min</code>,
     wird ein <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> ausgelöst.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.mt-rand-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Beschreibung</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
      <td>8.0.0</td>
      <td>
       Wenn <code class="parameter">max</code> kleiner ist als
       <code class="parameter">min</code>, wird ein <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>
       ausgelöst, wenn <code class="parameter">max</code> kleiner ist als
       <code class="parameter">min</code>; zuvor wurde ein <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
       erzeugt, und die Funktion gab <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> zurück.
      </td>
     </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><strong>mt_rand()</strong></span>
        <a href="migration72.incompatible.php#migration72.incompatible.rand-mt_rand-output" class="link">erhielt einen Bugfix</a>
        für einen Modulus-Verzerrungsfehler. Das bedeutet, dass sich Folgen
        mit einem bestimmten Startwert ab PHP 7.1 auf 64bit-Rechnern
        unterscheiden können.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><a href="function.rand.php" class="function">rand()</a></span>
        <a href="migration71.incompatible.php#migration71.incompatible.rand-srand-aliases" class="link">ist nun</a>
        ein Alias von <span class="function"><strong>mt_rand()</strong></span>.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><strong>mt_rand()</strong></span>
        <a href="migration71.incompatible.php#migration71.incompatible.fixes-to-mt_rand-algorithm" class="link"> verwendet nun</a>
        eine korrekte Version des Mersenne-Twister-Algorithmus. Um auf das
        vorherige Verhalten auszuweichen, kann <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span>
        mit <strong><code><a href="random.constants.php#constant.mt-rand-php">MT_RAND_PHP</a></code></strong> als zweitem Parameter verwendet
        werden.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mt-rand-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 <span class="function"><strong>mt_rand()</strong></span>-Beispiel</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">mt_rand</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">mt_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">mt_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>Das oben gezeigte Beispiel erzeugt
eine ähnliche Ausgabe wie:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">1604716014
1478613278
6</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mt-rand-notes">
  <h3 class="title">Anmerkungen</h3>
  <div class="warning"><strong class="warning">Warnung</strong>
   <p class="para">
    Der Bereich von <code class="parameter">min</code> und <code class="parameter">max</code>
    darf nicht größer sein als <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>. Das heißt,
    (<code class="parameter">max</code> - <code class="parameter">min</code>) &lt;=
    <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>. Andernfalls kann
    <span class="function"><strong>mt_rand()</strong></span> schlechtere Zufallszahlen liefern als es
    sollte.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mt-rand-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.mt-srand.php" class="function" rel="rdfs-seeAlso">mt_srand()</a> - Initialisiert den Mersenne-Twister-Zufallszahlengenerator</span></li>
    <li><span class="function"><a href="function.mt-getrandmax.php" class="function" rel="rdfs-seeAlso">mt_getrandmax()</a> - Liefert den gr&ouml;&szlig;tm&ouml;glichen Zufallswert</span></li>
    <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - Get a cryptographically secure, uniformly selected integer</span></li>
    <li><span class="function"><a href="function.random-bytes.php" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Get cryptographically secure random bytes</span></li>
   </ul>
  </p>
 </div>


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