Condividi tramite


Random.NextFloat Method

Definition

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

[Android.Runtime.Register("nextFloat", "()F", "GetNextFloatHandler")]
public virtual float NextFloat ();
[<Android.Runtime.Register("nextFloat", "()F", "GetNextFloatHandler")>]
abstract member NextFloat : unit -> single
override this.NextFloat : unit -> single

Returns

the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence

Attributes

Remarks

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

The general contract of nextFloat is that one float value, chosen (approximately) uniformly from the range 0.0f (inclusive) to 1.0f (exclusive), is pseudorandomly generated and returned. All 2<sup>24</sup> possible float values of the form m&nbsp;x&nbsp;2<sup>-24</sup>, where m is a positive integer less than 2<sup>24</sup>, are produced with (approximately) equal probability.

The method nextFloat is implemented by class Random as if by:

{@code
            public float nextFloat() {
              return next(24) / ((float)(1 << 24));
            }}

The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm shown would choose float values from the stated range with perfect uniformity.

[In early versions of Java, the result was incorrectly calculated as:

{@code
              return next(30) / ((float)(1 << 30));}

This might seem to be equivalent, if not better, but in fact it introduced a slight nonuniformity because of the bias in the rounding of floating-point numbers: it was slightly more likely that the low-order bit of the significand would be 0 than that it would be 1.]

Java documentation for java.util.Random.nextFloat().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to