App下載
首頁javaintstreamJava Stream - 如何生成隨機(jī)整數(shù)

Java Stream - 如何生成隨機(jī)整數(shù)

我們想知道如何生成隨機(jī)整數(shù)。
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
        SecureRandom secureRandom = new SecureRandom(new byte[]{1, 3, 3, 7});
        int[] randoms = IntStream.generate(secureRandom::nextInt)
                .filter(n -> n > 0)
                .limit(10)
                .toArray();
        System.out.println(Arrays.toString(randoms));


        int[] nums = IntStream.iterate(1, n -> n * 2)
                .limit(11)
                .toArray();
        System.out.println(Arrays.toString(nums));
    }
}