代码
1 Random rand = new Random();
2
3 // Random integers
4 int i = rand.nextInt();
5 // Continually call nextInt() for more random integers ...
6
7 // Random integers that range from from 0 to n
8 int n = 10;
9 i = rand.nextInt(n+1);
10
11 // Random bytes
12 byte[] bytes = new byte[5];
13 rand.nextBytes(bytes);
14
15 // Other primitive types
16 boolean b = rand.nextBoolean();
17 long l = rand.nextLong();
18 float f = rand.nextFloat(); // 0.0 <= f < 1.0
19 double d = rand.nextDouble(); // 0.0 <= d < 1.0
20
21
22 // Create two random number generators with the same seed
23 long seed = rand.nextLong();
24 rand = new Random(seed);
25 Random rand2 = new Random(seed);
26
2
3 // Random integers
4 int i = rand.nextInt();
5 // Continually call nextInt() for more random integers ...
6
7 // Random integers that range from from 0 to n
8 int n = 10;
9 i = rand.nextInt(n+1);
10
11 // Random bytes
12 byte[] bytes = new byte[5];
13 rand.nextBytes(bytes);
14
15 // Other primitive types
16 boolean b = rand.nextBoolean();
17 long l = rand.nextLong();
18 float f = rand.nextFloat(); // 0.0 <= f < 1.0
19 double d = rand.nextDouble(); // 0.0 <= d < 1.0
20
21
22 // Create two random number generators with the same seed
23 long seed = rand.nextLong();
24 rand = new Random(seed);
25 Random rand2 = new Random(seed);
26

浙公网安备 33010602011771号