在Java 8之前,隨機取數最常使用Math.randorm()的方法
在Java 8,Random類別新增很多方法來達到隨機取數,特別針對long, int, double基本型態,分別回傳LongStream、IntStream、DoubleStream
主要分為longs()
、ints()
、doubles()
三大類方法,這邊針對ints()
系列做介紹
Random類別ints()重載方法
public IntStream ints()
Returns an effectively unlimited stream of pseudorandom int values.
A pseudorandom int value is generated as if it's the result of calling the method nextInt().
Returns an effectively unlimited stream of pseudorandom int values.
A pseudorandom int value is generated as if it's the result of calling the method nextInt().
public IntStream ints(long streamSize)
Returns a stream producing the given streamSize number of pseudorandom int values.
A pseudorandom int value is generated as if it's the result of calling the method nextInt().
Returns a stream producing the given streamSize number of pseudorandom int values.
A pseudorandom int value is generated as if it's the result of calling the method nextInt().
public IntStream ints(int randomNumberOrigin, int randomNumberBound)
Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive)
Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive)
public int nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 2^32 possible int values are produced with (approximately) equal probability.
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 2^32 possible int values are produced with (approximately) equal probability.
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.
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
亂數實作練習
import java.util.Random;
import java.util.stream.IntStream;
public class RandomDemo {
public static void main(String[] args) {
Random r = new Random();
System.out.println("/* ints(long streamSize) */");
IntStream intStream = r.ints(5);
intStream.forEach(System.out::println);
System.out.println("/* ints(int randomNumberOrigin, int randomNumberBound) */");
//intStream = r.ints(0, 5);
//intStream.forEach(System.out::println);
System.out.println("/* ints(long streamSize, int randomNumberOrigin, int randomNumberBound) */");
intStream = r.ints(5, 0, 5);
intStream.forEach(System.out::println);
System.out.println("/* nextInt(int bound) */");
System.out.println(r.nextInt(5));
System.out.println("/* Math.random() */");
System.out.println((int)(Math.random()*5));
}
}
輸出
/* ints(long streamSize) */
1044769467
-1793543922
-74034918
1569331176
-1136474098
/* ints(int randomNumberOrigin, int randomNumberBound) */
/* ints(long streamSize, int randomNumberOrigin, int randomNumberBound) */
0
2
2
4
1
/* nextInt(int bound) */
1
/* Math.random() */
2
1044769467
-1793543922
-74034918
1569331176
-1136474098
/* ints(int randomNumberOrigin, int randomNumberBound) */
/* ints(long streamSize, int randomNumberOrigin, int randomNumberBound) */
0
2
2
4
1
/* nextInt(int bound) */
1
/* Math.random() */
2