⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javautil.doc7.html

📁 java语言规范
💻 HTML
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
 
<a href="index.html">Contents</a> | <a href="javautil.doc6.html">Prev</a> | <a href="javautil.doc8.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="7616"></a>
<center><h1>21.9  The Class  <code>java.util.Random</code></h1></center>
<a name="20141"></a>
Each instance of class <code>Random</code> serves as a separate, independent pseudorandom 
generator of primitive values.
<p><pre><a name="7617"></a>public class <code><b>Random</b></code> {
<a name="26608"></a>	protected long <code><b>seed</b></code>;
<a name="26609"></a>	protected double <code><b>nextNextGaussian</b></code>;
<a name="26610"></a>	protected boolean <code><b>haveNextNextGaussian</b></code> = false;
<a name="7618"></a>	public <code><b>Random</b></code>();
<a name="7619"></a>	public <code><b>Random</b></code>(long seed);
<a name="40724"></a>	public void <code><b>setSeed</b></code>(long seed);
<a name="40728"></a>	protected int <code><b>next</b></code>(int bits);
<a name="40725"></a>	public int <code><b>nextInt</b></code>();
<a name="40726"></a>	public long <code><b>nextLong</b></code>();
<a name="7623"></a>	public float <code><b>nextFloat</b></code>();
<a name="7624"></a>	public double <code><b>nextDouble</b></code>();
<a name="7625"></a>	public double <code><b>nextGaussian</b></code>();
<a name="7626"></a>}
</pre><a name="20145"></a>
If two <code>Random</code> objects are created with the same seed and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers in all Java implementations. In order to guarantee this property, particular algorithms are specified for the class <code>Random</code>. Java implementations must use all the algorithms shown here for the class <code>Random</code>, for the sake of absolute portability of Java code. However, subclasses of class <code>Random</code> are permitted use other algorithms, so long as they adhere to the general contracts for all the methods.<p>
<a name="24171"></a>
The algorithms implemented by class <code>Random</code> use three state variables, which are <code>protected</code>. They also use a <code>protected</code> utility method that on each invocation can supply up to up to 32 pseudorandomly generated bits.<p>
<a name="26624"></a>
<p><font size=+1><strong>21.9.1   </strong> <code>protected long <code><b>seed</b></code>;</code></font>
<p>
<a name="26627"></a>
A variable used by method <code>next</code> <a href="javautil.doc7.html#26664">(&#167;21.9.7)</a> to hold the state of the pseudorandom 
number generator.
<p><a name="26625"></a>
<p><font size=+1><strong>21.9.2   </strong> <code>protected double <code><b>nextNextGaussian</b></code>;</code></font>
<p>
<a name="26645"></a>
A variable used by method <code>nextGaussian</code> <a href="javautil.doc7.html#7634">(&#167;21.9.12)</a> to hold a precomputed 
value to be delivered by that method the next time it is called.
<p><a name="26622"></a>
<p><font size=+1><strong>21.9.3   </strong> <code>protected boolean <code><b>haveNextNextGaussian</b></code> = false;</code></font>
<p>
<a name="26655"></a>
A variable used by method <code>nextGaussian</code> <a href="javautil.doc7.html#7634">(&#167;21.9.12)</a> to keep track of whether it 
has precomputed and stashed away the next value to be delivered by that method.
<p><a name="7627"></a>
<p><font size=+1><strong>21.9.4   </strong> <code>public <code><b>Random</b></code>()</code></font>
<p>
<a name="20026"></a>
This constructor initializes a newly created <code>Random</code> number generator by using the 
current time of day <a href="javalang.doc17.html#5984">(&#167;20.18.6)</a> as a seed.
<p><pre><a name="24245"></a>public Random() { this(System.currentTimeMillis()); }
</pre><a name="7628"></a>
<p><font size=+1><strong>21.9.5   </strong> <code>public <code><b>Random</b></code>(long seed)</code></font>
<p>
<a name="20041"></a>
This constructor initializes a newly created <code>Random</code> number generator by using the 
argument <code>seed</code> as a seed.
<p><pre><a name="24255"></a>
public Random(long seed) { setSeed(seed); }
</pre><a name="26079"></a>
<p><font size=+1><strong>21.9.6   </strong> <code>public void <code><b>setSeed</b></code>(long seed)</code></font>
<p>
<a name="26080"></a>
The general contract of <code>setSeed</code> is that it alters the state of this random number 
generator object so as to be in exactly the same state as if it had just been created 
with the argument <code>seed</code> as a seed.
<p><a name="26081"></a>
The method <code>setSeed</code> is implemented by class <code>Random</code> as follows:<p>
<pre><a name="26082"></a>
synchronized public void setSeed(long seed) {
<a name="26083"></a>	this.seed = (seed ^ 0x5DEECE66DL) &amp; ((1L &lt;&lt; 48) - 1);
<a name="26089"></a>	haveNextNextGaussian = false;
<a name="26084"></a>}
</pre><a name="26085"></a>
The implementation of <code>setSeed</code> by class <code>Random</code> happens to use only 48 bits of 
the given seed. In general, however, an overriding method may use all 64 bits of 
the long argument as a seed value.
<p><a name="27569"></a>
[In certain early versions of Java, the <code>setSeed</code> method failed to reset the value of <code>haveNextNextGaussian</code> to <code>false</code>; this flaw could lead to failure to produce repeatable behavior.]<p>
<a name="26664"></a>
<p><font size=+1><strong>21.9.7   </strong> <code>protected int <code><b>next</b></code>(int bits)</code></font>
<p>
<a name="26672"></a>
The general contract of <code>next</code> is that it returns an <code>int</code> value and if the argument 
bits is between <code>1</code> and <code>32</code> (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</code> or <code>1</code>.
<p><a name="26682"></a>
The method <code>next</code> is implemented by class <code>Random</code> as follows:<p>
<pre><a name="26665"></a>
synchronized protected int next(int bits) {
<a name="26666"></a>	seed = (seed * 0x5DEECE66DL + 0xBL) &amp; ((1L &lt;&lt; 48) - 1);
<a name="26667"></a>	return (int)(seed &gt;&gt;&gt; (48 - bits));
<a name="26668"></a>}
</pre><a name="26669"></a>
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 2: <i>Seminumerical Algorithms</i>, section 3.2.1.
<p><a name="7630"></a>
<p><font size=+1><strong>21.9.8   </strong> <code>public int <code><b>nextInt</b></code>()</code></font>
<p>
<a name="20072"></a>
The general contract of <code>nextInt</code> is that one <code>int</code> value is pseudorandomly generated
and returned. All <img src="javautil.doc.anc.gif">possible <code>int</code> values are produced with (approximately) 
equal probability.
<p><a name="24391"></a>
The method <code>setSeed</code> is implemented by class <code>Random</code> as follows:<p>
<pre><a name="24389"></a>public int nextInt() {  return next(32); }
</pre><a name="7631"></a>
<p><font size=+1><strong>21.9.9   </strong> <code>public long <code><b>nextLong</b></code>()</code></font>
<p>
<a name="20086"></a>
The general contract of <code>nextLong</code> is that one <code>long</code> value is pseudorandomly generated
and returned. All <img src="javautil.doc.anc1.gif">possible <code>long</code> values are produced with (approximately)
equal probability.
<p><a name="24395"></a>
The method <code>setSeed</code> is implemented by class <code>Random</code> as follows:<p>
<pre><a name="24410"></a>
public long nextLong() {
<a name="24422"></a>	return ((long)next(32) &lt;&lt; 32) + next(32);
<a name="24413"></a>}
</pre><a name="7632"></a>
<p><font size=+1><strong>21.9.10   </strong> <code>public float <code><b>nextFloat</b></code>()</code></font>
<p>
<a name="24359"></a>
The general contract of <code>nextFloat</code> is that one <code>float</code> value, chosen (approximately)
uniformly from the range <code>0.0f</code> (inclusive) to <code>1.0f</code> (exclusive), is pseudorandomly
generated and returned. All <img src="javautil.doc.anc6.gif">possible <code>float</code> values of the form 
<img src="javautil.doc.anc7.gif">, where <i>m</i> is a positive integer less than <img src="javautil.doc.anc8.gif">, are produced with (approximately)
equal probability.
<p><a name="24398"></a>
The method <code>setSeed</code> is implemented by class <code>Random</code> as follows:<p>
<pre><a name="24427"></a>
public float nextFloat() {
<a name="24454"></a>	return next(24) / ((float)(1 &lt;&lt; 24));
<a name="24453"></a>}
</pre><a name="27572"></a>
The hedge "approximately" is used in the foregoing description only because the 
<code>next</code> method is only approximately an unbiased source of independently chosen 
bits. If it were a perfect source or randomly chosen bits, then the algorithm shown 
would choose <code>float</code> values from the stated range with perfect uniformity.
<p><a name="27608"></a>
[In early versions of Java, the result was incorrectly calculated as:<p>
<pre><a name="27580"></a>	return next(30) / ((float)(1 &lt;&lt; 30));
</pre><a name="27577"></a>
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 <code>0</code> than 
that it would be <code>1</code>.]
<p><a name="7633"></a>
<p><font size=+1><strong>21.9.11   </strong> <code>public double <code><b>nextDouble</b></code>()</code></font>
<p>
<a name="20100"></a>
The general contract of <code>nextDouble</code> is that one <code>double</code> value, chosen (approximately)
uniformly from the range <code>0.0d</code> (inclusive) to <code>1.0d</code> (exclusive), is pseudorandomly
generated and returned. All <img src="javautil.doc.anc9.gif">possible <code>float</code> values of the form 
<img src="javautil.doc.anc10.gif">, where <i>m</i> is a positive integer less than <img src="javautil.doc.anc11.gif">, are produced with (approximately)
equal probability.
<p><a name="24478"></a>
The method <code>setSeed</code> is implemented by class <code>Random</code> as follows:<p>
<pre><a name="24484"></a>
public double nextDouble() {
<a name="24486"></a>	return (((long)next(26) &lt;&lt; 27) + next(27))
<a name="24512"></a>				/ (double)(1L &lt;&lt; 53);
<a name="24481"></a>}
</pre><a name="27650"></a>
The hedge "approximately" is used in the foregoing description only because the 
<code>next</code> method is only approximately an unbiased source of independently chosen 
bits. If it were a perfect source or randomly chosen bits, then the algorithm shown 
would choose <code>double</code> values from the stated range with perfect uniformity.
<p><a name="27651"></a>
[In early versions of Java, the result was incorrectly calculated as:<p>
<pre><a name="27662"></a>
	return (((long)next(27) &lt;&lt; 27) + next(27))
<a name="27663"></a>				/ (double)(1L &lt;&lt; 54);
</pre><a name="27655"></a>
This might seem to be equivalent, if not better, but in fact it introduced a large 
nonuniformity because of the bias in the rounding of floating-point numbers: it 
was three times as likely that the low-order bit of the significand would be <code>0</code> than 
that it would be <code>1</code>! This nonuniformity probably doesn't matter much in practice, 
but we strive for perfection.]
<p><a name="7634"></a>
<p><font size=+1><strong>21.9.12   </strong> <code>public double <code><b>nextGaussian</b></code>()</code></font>
<p>
<a name="20130"></a>
The general contract of <code>nextGaussian</code> is that one <code>double</code> value, chosen from 
(approximately) the usual normal distribution with mean <code>0.0</code> and standard deviation
<code>1.0</code>, is pseudorandomly generated and returned.
<p><a name="24405"></a>
The method <code>setSeed</code> is implemented by class <code>Random</code> as follows:<p>
<pre><a name="24527"></a>
synchronized public double nextGaussian() {
<a name="24529"></a>	if (haveNextNextGaussian) {
<a name="24568"></a>		haveNextNextGaussian = false;
<a name="24531"></a>		return nextNextGaussian;
<a name="24532"></a>	} else {
<a name="24533"></a>		double v1, v2, s;
<a name="24534"></a>		do { 
<a name="24535"></a>			v1 = 2 * nextDouble() - 1; 	// between -1.0 and 1.0
<a name="24536"></a>			v2 = 2 * nextDouble() - 1; 	// between -1.0 and 1.0
<a name="24537"></a>			s = v1 * v1 + v2 * v2;
<a name="24538"></a>		} while (s &gt;= 1);
<a name="24539"></a>		double norm = Math.sqrt(-2 * Math.log(s)/s);
<a name="24540"></a>		nextNextGaussian = v2 * norm;
<a name="24541"></a>		haveNextNextGaussian = true;
<a name="24542"></a>		return v1 * norm;
<a name="24543"></a>	}
<a name="25681"></a>}
</pre><a name="25682"></a>
This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and G. Marsaglia, as 
described by Donald E. Knuth in <i>The Art of Computer Programming</i>, Volume 2: 
<i>Seminumerical Algorithms</i>, section 3.4.1, subsection C, algorithm P. Note that it 
generates two independent values at the cost of only one call to <code>Math.log</code> and one 
call to <code>Math.sqrt</code>.
<p><a name="25689"></a>
<img src="javautil.doc.anc5.gif"><p>


<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javautil.doc6.html">Prev</a> | <a href="javautil.doc8.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright &#169 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -