<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.random-randomizer.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'random-randomizer.nextfloat.php',
    1 => 'Random\\Randomizer::nextFloat',
    2 => 'Get a float from the right-open interval [0.0, 1.0)',
  ),
  'up' => 
  array (
    0 => 'class.random-randomizer.php',
    1 => 'Random\\Randomizer',
  ),
  'prev' => 
  array (
    0 => 'random-randomizer.getint.php',
    1 => 'Random\\Randomizer::getInt',
  ),
  'next' => 
  array (
    0 => 'random-randomizer.nextint.php',
    1 => 'Random\\Randomizer::nextInt',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/random/random/randomizer/nextfloat.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="random-randomizer.nextfloat" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Random\Randomizer::nextFloat</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.3.0)</p><p class="refpurpose"><span class="refname">Random\Randomizer::nextFloat</span> &mdash; <span class="dc-title">Get a float from the right-open interval [0.0, 1.0)</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-random-randomizer.nextfloat-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Random\Randomizer::nextFloat</strong></span>(): <span class="type"><a href="language.types.float.php" class="type float">float</a></span></div>

  <p class="para rdfs-comment">
   Returns a uniformly selected, equidistributed float from the right-open
   interval from <code class="literal">0.0</code> until, but not including,
   <code class="literal">1.0</code>.
  </p>

  <p class="para">
   The chance for returned float to be within a given right-open sub-interval
   is proportional to the size of the sub-interval.

   This means that the chance for a float to be <em>less than</em> <code class="literal">0.5</code>
   is 50 %, which is equal to the chance for a float to be <em>at least</em> <code class="literal">0.5</code>.

   Likewise the chance for a float to be within the right-open interval from
   <code class="literal">0.2</code> until, but not including, <code class="literal">0.25</code> is
   exactly 5 %.
  </p>

  <p class="para">
   This property makes it easy to leverage <span class="methodname"><strong>Random\Randomizer::nextFloat()</strong></span>
   to generate a random boolean with a given chance by checking if the returned float is
   <em>less than</em> a given chance.
  </p>

  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <p class="para">
    The domain of the returnable floats of <span class="methodname"><strong>Random\Randomizer::nextFloat()</strong></span>
    is identical to that of <code class="code">Randomizer::getFloat(0.0, 1.0, IntervalBoundary::ClosedOpen)</code>.
   </p>

   <p class="para">
    The internal implementation of <span class="methodname"><strong>Random\Randomizer::nextFloat()</strong></span> is more
    efficient.
   </p>
  </p></blockquote>

  <div class="caution"><strong class="caution">Attenzione</strong>
   <p class="para">
    Scaling the return value to a different interval using multiplication
    or addition (a so-called affine transformation) might result in a bias
    in the resulting value as floats are not equally dense across the number
    line. As not all values can be exactly represented by a float, the
    result of the affine transformation might also result in values outside
    of the requested interval due to implicit rounding.

    An <a href="random-randomizer.getfloat.php#random-randomizer.getfloat.affine-transformation" class="link">in-depth explanation</a>
    of the problems with the affine transformation is given in the documentation
    for <span class="methodname"><a href="random-randomizer.getfloat.php" class="methodname">Random\Randomizer::getFloat()</a></span>.
   </p>
   <p class="para">
    Use <span class="methodname"><a href="random-randomizer.getfloat.php" class="methodname">Random\Randomizer::getFloat()</a></span> to generate a
    random float within an arbitrary interval. Use <span class="methodname"><a href="random-randomizer.getint.php" class="methodname">Random\Randomizer::getInt()</a></span>
    to generate a random integer within an arbitrary interval.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-random-randomizer.nextfloat-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">Questa funzione non contiene parametri.</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-random-randomizer.nextfloat-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   A uniformly selected, equidistributed float from the right-open (<code class="literal">IntervalBoundary::ClosedOpen</code>)
   interval [0.0, 1.0).

   <code class="literal">0.0</code> is a possible return value, <code class="literal">1.0</code> is not.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-random-randomizer.nextfloat-errors">
  <h3 class="title">Errori/Eccezioni</h3>
  <ul class="itemizedlist">
   
 <li class="listitem">
  <span class="simpara">
   Any <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span>s thrown by the <span class="methodname"><a href="random-engine.generate.php" class="methodname">Random\Engine::generate()</a></span> method
   of the underlying <a href="class.random-randomizer.php#random-randomizer.props.engine" class="link"><code class="literal">Random\Randomizer::$engine</code></a>.
  </span>
 </li>

  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-random-randomizer.nextfloat-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="methodname"><strong>Random\Randomizer::nextFloat()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$r </span><span style="color: #007700">= new </span><span style="color: #0000BB">\Random\Randomizer</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// The resulting bool will be true with the given chance.<br /></span><span style="color: #0000BB">$chance </span><span style="color: #007700">= </span><span style="color: #0000BB">0.5</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$bool </span><span style="color: #007700">= </span><span style="color: #0000BB">$r</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nextFloat</span><span style="color: #007700">() &lt; </span><span style="color: #0000BB">$chance</span><span style="color: #007700">;<br /><br />echo (</span><span style="color: #0000BB">$bool </span><span style="color: #007700">? </span><span style="color: #DD0000">"You won" </span><span style="color: #007700">: </span><span style="color: #DD0000">"You lost"</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>Il precedente esempio visualizzerà
qualcosa simile a:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">You won</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-2">
   <p><strong>Example #2 Incorrect scaling using an affine transformation</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">final class </span><span style="color: #0000BB">MaxEngine </span><span style="color: #007700">implements </span><span style="color: #0000BB">Random\Engine </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">generate</span><span style="color: #007700">(): </span><span style="color: #0000BB">string </span><span style="color: #007700">{<br />        return </span><span style="color: #DD0000">"\xff"</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$randomizer </span><span style="color: #007700">= new </span><span style="color: #0000BB">\Random\Randomizer</span><span style="color: #007700">(new </span><span style="color: #0000BB">MaxEngine</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$min </span><span style="color: #007700">= </span><span style="color: #0000BB">3.5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$max </span><span style="color: #007700">= </span><span style="color: #0000BB">4.5</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// DO NOT DO THIS:<br />//<br />// This will output 4.5, despite nextFloat() sampling from<br />// a right-open interval, which will never return 1.<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Wrong scaling: %.17g"</span><span style="color: #007700">, </span><span style="color: #0000BB">$randomizer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nextFloat</span><span style="color: #007700">() * (</span><span style="color: #0000BB">$max </span><span style="color: #007700">- </span><span style="color: #0000BB">$min</span><span style="color: #007700">) + </span><span style="color: #0000BB">$min</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Correct:<br />// $randomizer-&gt;getFloat($min, $max, \Random\IntervalBoundary::ClosedOpen);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Il precedente esempio visualizzerà:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Wrong scaling: 4.5</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-random-randomizer.nextfloat-seealso">
  <h3 class="title">Vedere anche:</h3>
  <ul class="simplelist">
   <li><span class="methodname"><a href="random-randomizer.getfloat.php" class="methodname" rel="rdfs-seeAlso">Random\Randomizer::getFloat()</a> - Get a uniformly selected float</span></li>
  </ul>
 </div>


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