查API,学新内容--- (零) 随机数
查API,学新内容— (零) 随机数
0. 写在最前面
最近打算将空闲时间利用起来,查API,去学习自己还不是非常了解的内容,并把查到,学到的内容写下来,希望在这个过程可以提高自己的水平.首篇只作为尝试.
1. Random类
/**
* public class Arrays
* extends Object
* This class contains various methods for manipulating arrays (such as sorting and searching).
* This class also contains a static factory that allows arrays to be viewed as lists.
* The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.
*
* The documentation for the methods contained in this class includes briefs description of the implementations.
* Such descriptions should be regarded as implementation notes, rather than parts of the specification.
* Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to.
* (For example, the algorithm used by sort(Object[]) does not have to be a MergeSort, but it does have to be stable.)
*
* This class is a member of the Java Collections Framework.
*
* Since:
* 1.2
*
* 此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。
*
* 除非特别注明,否则如果指定数组引用为 null,则此类中的方法都会抛出 NullPointerException。
*
* 此类中所含方法的文档都包括对实现 的简短描述。应该将这些描述视为实现注意事项,而不应将它们视为规范 的一部分。实现者应该可以随意替代其他算法,只要遵循规范本身即可。(例如,sort(Object[]) 使用的算法不必是一个合并排序算法,但它必须是稳定的。)
*
* 此类是 Java Collections Framework 的成员。
*
* 从以下版本开始:
* 1.2
*/
2. nextInt(int)方法
/**
* public int nextInt(int bound)
* Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive),
* drawn from this random number generator's sequence.
* The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned.
* All bound possible int values are produced with (approximately) equal probability.
* The method nextInt(int bound) is implemented by class Random as if by:
*
* public int nextInt(int bound) {
* if (bound <= 0)
* throw new IllegalArgumentException("bound must be positive");
*
* if ((bound & -bound) == bound) // i.e., bound is a power of 2
* return (int)((bound * (long)next(31)) >> 31);
*
* int bits, val;
* do {
* bits = next(31);
* val = bits % bound;
* } while (bits - val + (bound-1) < 0);
* return val;
* }
* 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 int values from the stated range with perfect uniformity.
*
* 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.
*
* 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 low-order 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.
*
* Parameters:
* bound - the upper bound (exclusive). Must be positive.
* Returns:
* the next pseudorandom, uniformly distributed int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence
* Throws:
* IllegalArgumentException - if bound is not positive
* Since:
* 1.2
*
* public int nextInt(int n)
* 返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。nextInt 的常规协定是,伪随机地生成并返回指定范围中的一个 int 值。所有可能的 n 个 int 值的生成概率(大致)相同。Random 类按如下方式实现 nextInt(int n) 方法:
* 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;
* }
* 前面的描述中使用了不确定的词“大致”,因为 next 方法只是一个大致上独自选择位的无偏源。如果它是一个随机选择位的最佳源,那么给出的算法应该从规定范围完全一致地选择 int 值。
*
* 该算法稍微有些复杂。它拒绝那些会导致不均匀分布的值(由于 2^31 无法被 n 整除)。某个值被拒绝的概率取决于 n。最坏的情况是 n=2^30+1,拒绝的概率是 1/2,循环终止前的预计迭代次数是 2。
*
* 该算法特别对待 n 是 2 的次幂的情况:它从底层伪随机数生成器中返回正确的高位数。在不是特殊处理的情况中,将返回正确的低 位数。
* 众所周知,线性同余伪随机数生成器(比如此类所实现的)在其低位的值序列中周期较短。因此,如果 n 是 2 的次幂(幂值较小),则这种特殊情况将大大增加此方法的后续调用所返回的值序列长度。
*
* 参数:
* n - 要返回的随机数的范围。必须为正数。
* 返回:
* 下一个伪随机数,在此随机数生成器序列中 0(包括)和 n(不包括)之间均匀分布的 int 值。
* 抛出:
* IllegalArgumentException - 如果 n 不是正数
* 从以下版本开始:
* 1.2
*/
// 下面为示例代码
Random random = new Random();
int a = random.nextInt(10);
System.out.println(a);