|
Random\Randomizer::nextFloatGet a float from the right-open interval [0.0, 1.0) Description
public float Random\Randomizer::nextFloat()
Returns a uniformly selected, equidistributed float from the right-open
interval from
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 less than This property makes it easy to leverage Random\Randomizer::nextFloat to generate a random boolean with a given chance by checking if the returned float is less than a given chance.
Caution
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 in-depth explanation of the problems with the affine transformation is given in the documentation for Random\Randomizer::getFloat. Use Random\Randomizer::getFloat to generate a random float within an arbitrary interval. Use Random\Randomizer::getInt to generate a random integer within an arbitrary interval. ParametersThis function has no parameters. Return Values
A uniformly selected, equidistributed float from the right-open ( Errors/Exceptions
ExamplesExample #1 Random\Randomizer::nextFloat example
The above example will output something similar to: You won Example #2 Incorrect scaling using an affine transformation
The above example will output: Wrong scaling: 4.5 See Also
|