📄 random.html
字号:
</TR></TABLE> <A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor"><TD><B>Methods inherited from class java.lang.<A HREF="../../java/lang/Object.html">Object</A></B></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><A HREF="../../java/lang/Object.html#clone()">clone</A>, <A HREF="../../java/lang/Object.html#equals(java.lang.Object)">equals</A>, <A HREF="../../java/lang/Object.html#finalize()">finalize</A>, <A HREF="../../java/lang/Object.html#getClass()">getClass</A>, <A HREF="../../java/lang/Object.html#hashCode()">hashCode</A>, <A HREF="../../java/lang/Object.html#notify()">notify</A>, <A HREF="../../java/lang/Object.html#notifyAll()">notifyAll</A>, <A HREF="../../java/lang/Object.html#toString()">toString</A>, <A HREF="../../java/lang/Object.html#wait()">wait</A>, <A HREF="../../java/lang/Object.html#wait(long)">wait</A>, <A HREF="../../java/lang/Object.html#wait(long, int)">wait</A></CODE></TD></TR></TABLE> <P><!-- ============ FIELD DETAIL =========== --><!-- ========= CONSTRUCTOR DETAIL ======== --><A NAME="constructor_detail"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=1><FONT SIZE="+2"><B>Constructor Detail</B></FONT></TD></TR></TABLE><A NAME="Random()"><!-- --></A><H3>Random</H3><PRE>public <B>Random</B>()</PRE><DL><DD>Creates a new random number generator. Its seed is initialized to a value based on the current time: <blockquote><pre> public Random() { this(System.currentTimeMillis()); }</pre></blockquote><DD><DL><DT><B>See Also: </B><DD><A HREF="../../java/lang/System.html#currentTimeMillis()"><CODE>System.currentTimeMillis()</CODE></A></DL></DD></DL><HR><A NAME="Random(long)"><!-- --></A><H3>Random</H3><PRE>public <B>Random</B>(long seed)</PRE><DL><DD>Creates a new random number generator using a single <code>long</code> seed: <blockquote><pre> public Random(long seed) { setSeed(seed); }</pre></blockquote> Used by method <tt>next</tt> to hold the state of the pseudorandom number generator.<DD><DL><DT><B>Parameters:</B><DD><CODE>seed</CODE> - the initial seed.<DT><B>See Also: </B><DD><A HREF="../../java/util/Random.html#setSeed(long)"><CODE>setSeed(long)</CODE></A></DL></DD></DL><!-- ============ METHOD DETAIL ========== --><A NAME="method_detail"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=1><FONT SIZE="+2"><B>Method Detail</B></FONT></TD></TR></TABLE><A NAME="setSeed(long)"><!-- --></A><H3>setSeed</H3><PRE>public void <B>setSeed</B>(long seed)</PRE><DL><DD>Sets the seed of this random number generator using a single <code>long</code> seed. The general contract of <tt>setSeed</tt> 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 <tt>seed</tt> as a seed. The method <tt>setSeed</tt> is implemented by class Random as follows: <blockquote><pre> synchronized public void setSeed(long seed) { this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1); haveNextNextGaussian = false; }</pre></blockquote> The implementation of <tt>setSeed</tt> by class <tt>Random</tt> 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.<DD><DL></DL></DD><DD><DL><DT><B>Parameters:</B><DD><CODE>seed</CODE> - the initial seed.</DL></DD></DL><HR><A NAME="next(int)"><!-- --></A><H3>next</H3><PRE>protected int <B>next</B>(int bits)</PRE><DL><DD>Generates the next pseudorandom number. Subclass should override this, as this is used by all other methods.<p> The general contract of <tt>next</tt> is that it returns an <tt>int</tt> value and if the argument bits is between <tt>1</tt> and <tt>32</tt> (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 <tt>0</tt> or <tt>1</tt>. The method <tt>next</tt> is implemented by class <tt>Random</tt> as follows: <blockquote><pre> synchronized protected int next(int bits) { seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); return (int)(seed >>> (48 - bits)); }</pre></blockquote> 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.<DD><DL></DL></DD><DD><DL><DT><B>Parameters:</B><DD><CODE>bits</CODE> - random bits<DT><B>Returns:</B><DD>the next pseudorandom value from this random number generator's sequence.<DT><B>Since: </B><DD>JDK1.1</DD></DL></DD></DL><HR><A NAME="nextBytes(byte[])"><!-- --></A><H3>nextBytes</H3><PRE>public void <B>nextBytes</B>(byte[] bytes)</PRE><DL><DD>Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.<DD><DL></DL></DD><DD><DL><DT><B>Parameters:</B><DD><CODE>bytes</CODE> - the non-null byte array in which to put the random bytes.<DT><B>Since: </B><DD>JDK1.1</DD></DL></DD></DL><HR><A NAME="nextInt()"><!-- --></A><H3>nextInt</H3><PRE>public int <B>nextInt</B>()</PRE><DL><DD>Returns the next pseudorandom, uniformly distributed <code>int</code> value from this random number generator's sequence. The general contract of <tt>nextInt</tt> is that one <tt>int</tt> value is pseudorandomly generated and returned. All 2<font size="-1"><sup>32 </sup></font> possible <tt>int</tt> values are produced with (approximately) equal probability. The method <tt>nextInt</tt> is implemented by class <tt>Random</tt> as follows: <blockquote><pre> public int nextInt() { return next(32); }</pre></blockquote><DD><DL></DL></DD><DD><DL><DT><B>Returns:</B><DD>the next pseudorandom, uniformly distributed <code>int</code> value from this random number generator's sequence.</DL></DD></DL><HR><A NAME="nextInt(int)"><!-- --></A><H3>nextInt</H3><PRE>public int <B>nextInt</B>(int n)</PRE><DL><DD>Returns a pseudorandom, uniformly distributed <tt>int</tt> value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of <tt>nextInt</tt> is that one <tt>int</tt> value in the specified range is pseudorandomly generated and returned. All <tt>n</tt> possible <tt>int</tt> values are produced with (approximately) equal probability. The method <tt>nextInt(int n)</tt> is implemented by class <tt>Random</tt> as follows: <blockquote><pre> public int nextInt(int n) { if (n<=0) throw new IllegalArgumentException("n must be positive"); if ((n & -n) == n) // i.e., n is a power of 2 return (int)((n * (long)next(31)) >> 31); int bits, val; do { bits = next(31); val = bits % n; } while(bits - val + (n-1) < 0); return val; } </pre></blockquote> <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 <tt>int</tt> values from the stated range with perfect uniformity. <p> The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2. <p> The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number of <i>low-order</i> bits would be returned. Linear congruential pseudo-random number generators such as the one implemented by this class are known to have short periods in the sequence of values of their low-order bits. Thus, this special case greatly increases the length of the sequence of values returned by successive calls to this method if n is a small power of two.<DD><DL></DL></DD><DD><DL><DT><B>Parameters:</B><DD><CODE>n</CODE> - the bound on the random number to be returned. Must be positive.<DT><B>Returns:</B><DD>a pseudorandom, uniformly distributed <tt>int</tt> value between 0 (inclusive) and n (exclusive).<DT><B>Throws:</B><DD><CODE><A HREF="../../java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - n is not positive.<DT><B>Since: </B><DD>1.2</DD></DL></DD></DL><HR><A NAME="nextLong()"><!-- --></A><H3>nextLong</H3><PRE>public long <B>nextLong</B>()</PRE><DL><DD>Returns the next pseudorandom, uniformly distributed <code>long</code>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -