Addendum:
Dando uno sguardo al codice java.util.Random
sorgente incluso con la distribuzione di Oracle JDK 7 ("Copyright (c) ..., 1995, 2010, Oracle e/o delle sue affiliate Tutti i diritti riservati ORACLE PROPRIETÀ/CONFIDENTIAL uso è soggetto ai termini di licenza") mostra questo semplice codice:
class Random {
public float nextFloat() {
return next(24)/((float)(1 << 24));
}
protected int next(int bits) {
long oldseed, nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed, nextseed));
return (int)(nextseed >>> (48 - bits));
}
}
Così, per 0.123.167,633293 millions:
- prendere un "valore intero casuale" tra 0 e 2^24-1 (o meglio, un casuale 24 bit bitpattern interpretato come un valore intero),
- Convertire a galleggiare (in Java "float" ha il mandato di essere un IEEE 724 a 32 bit in virgola mobile, che può rappresentare fino a 2^24 senza perdita di precisione, e quindi sarà un valore compreso tra 0 e 1.6777215E7)
- Quindi dividerlo per la rappresentazione float di 2^24, nuovamente appena rappresentabile senza perdita di precisione come 1.6777216E7. 2^24 + 1 = 16777217 scenderebbe a 1.6777216E7 quando forzato a essere float. Nel codice, questo dovrebbe essere davvero una costante. Hey Sun, i cicli non crescono sugli alberi !!
- La divisione risulta in un float in [0.0 .. 0.99999994] (il risultato della divisione corretta sarebbe intorno a 0.999999940395355224609375), con, penso, tutti i possibili valori in virgola mobile IEEE 724 tra "ugualmente possibile".
Vedere anche IEEE floating point e Floating-Point Arithmetic on the JVM.
commenti Javadoc per `prossimo() è:
/**
* Generates the next pseudorandom number. Subclasses should
* override this, as this is used by all other methods.
*
* <p>The general contract of {@code next} is that it returns an
* {@code int} value and if the argument {@code bits} is between
* {@code 1} and {@code 32} (inclusive), then that many low-order
* bits of the returned value will be (approximately) independently
* chosen bit values, each of which is (approximately) equally
* likely to be {@code 0} or {@code 1}. The method {@code next} is
* implemented by class {@code Random} by atomically updating the seed to
* <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
* and returning
* <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
*
* This is a linear congruential pseudorandom number generator, as
* defined by D. H. Lehmer and described by Donald E. Knuth in
* <i>The Art of Computer Programming,</i> Volume 3:
* <i>Seminumerical Algorithms</i>, section 3.2.1.
*
* @param bits random bits
* @return the next pseudorandom value from this random number
* generator's sequence
* @since 1.1
*/
I commenti Javadoc per nextFloat()
è:
/**
* Returns the next pseudorandom, uniformly distributed {@code float}
* value between {@code 0.0} and {@code 1.0} from this random
* number generator's sequence.
*
* <p>The general contract of {@code nextFloat} is that one
* {@code float} value, chosen (approximately) uniformly from the
* range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
* pseudorandomly generated and returned. All 2<font
* size="-1"><sup>24</sup></font> possible {@code float} values
* of the form <i>m x </i>2<font
* size="-1"><sup>-24</sup></font>, where <i>m</i> is a positive
* integer less than 2<font size="-1"><sup>24</sup> </font>, are
* produced with (approximately) equal probability.
*
* <p>The method {@code nextFloat} is implemented by class {@code Random}
* as if by:
* <pre> {@code
* public float nextFloat() {
* return next(24)/((float)(1 << 24));
* }}</pre>
*
* <p>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 {@code float}
* values from the stated range with perfect uniformity.<p>
* [In early versions of Java, the result was incorrectly calculated as:
* <pre> {@code
* return next(30)/((float)(1 << 30));}</pre>
* 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.]
*
* @return the next pseudorandom, uniformly distributed {@code float}
* value between {@code 0.0} and {@code 1.0} from this
* random number generator's sequence
*/
definire casuale :) –
mi chiedo quale sia l'uso di includere specificamente 1? – bryantsai
È casuale; solo non uniformemente casuale. –