Constructor Summary | |
Random() Creates a new random number generator. | |
Random(long seed) Creates a new random number generator using a single long seed: |
Method Summary | |
protected int | next(int bits) Generates the next pseudorandom number. |
boolean | nextBoolean() Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. |
void | nextBytes(byte[] bytes) Generates random bytes and places them into a user-supplied byte array. |
double | nextDouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. |
float | nextFloat() Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence. |
double | nextGaussian() Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence. |
int | nextInt() Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. |
int | nextInt(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. |
long | nextLong() Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. |
void | setSeed(long seed) Sets the seed of this random number generator using a single long seed. |
使用实例:
import java.util.Random;
public class RandomNum{
public static void main(String[] args) {
Random ran=new Random();
System.out.println(ran.nextInt());//产生任意大小的随机整数
System.out.println(ran.nextBoolean());//
System.out.println(ran.nextDouble());//
System.out.println(ran.nextFloat());//
System.out.println(ran.nextLong());//
System.out.println(ran.nextInt(100));//产生0至100的随机整数
}
}
结果:
-1116918815
false
0.3570466757532643
0.69238234
7423008150361503085
88
由于结果是随记的每次运行的结果不同