One of the most obvious changes in Java8 to the java.util.Random class is the addition of methods that return Random streams of numbers.

The following code creates an infinite stream of numbers of type double between 0 (inclusive) and 1 (inclusive).

Random random = new Random();
DoubleStream doubleStream = random.doubles(); 
Copy the code

The following code creates an infinite stream of numbers of type int between 0 (inclusive) and 100 (excluding 100).

Random random = new Random();
IntStream intStream = random.ints(0, 100); 
Copy the code

So what does this endless stream of numbers do? Next, I will analyze it through some cases. Remember, these infinite streams of numbers can only be truncated in some way (limited).

Example 1: Create 10 random integer streams and print them out:

intStream.limit(10).forEach(System.out::println);
Copy the code

Example 2: Create 100 random integers:

List<Integer> randomBetween0And99 = intStream
                                       .limit(100)
                                       .boxed()
                                       .collect(Collectors.toList()); 
Copy the code

For Gaussian pseudo-random values, streams created by the random.doubles() method are not equivalent to Gaussian pseudo-random numbers, however, the functionality provided by Java8 is very easy to implement.

Random random = new Random();
DoubleStream gaussianStream = Stream.generate(random::nextGaussian).mapToDouble(e -> e); 
Copy the code

Here, I use the Stream.generate API and pass in as a parameter an object from the Supplier class that creates another Gaussian pseudorandom number by calling nextGaussian() in the Random class.

Next, let’s do a more interesting thing for the pseudorandom stream of type double and the Gaussian pseudorandom stream of type double, which is to get the distribution of random numbers for the two streams. The expected result is that double pseudorandom numbers are evenly distributed, while Double Gaussian pseudorandom numbers should be normally distributed.

I generate a million pseudo-random numbers using the following code, which is implemented using the API provided by java8:

Random random = new Random(); DoubleStream DoubleStream = doubles(-1.0, 1.0); DoubleStream DoubleStream = Doubles (-1.0, 1.0); LinkedHashMap<Range, Integer> rangeCountMap = doubleStream.limit(1000000) .boxed() .map(Ranges::of) .collect(Ranges::emptyRangeCountMap, (m, e) -> m.put(e, m.get(e) + 1), Ranges::mergeRangeCountMaps); rangeCountMap.forEach((k, v) -> System.out.println(k.from() +"\t" + v)); 
Copy the code

The code runs as follows:

-1 49730-0.9 49931-0.8 50057-0.7 50060-0.6 49963-0.5 50159 -0.4 49921-0.3 49962-0.2 50231 -0.1 49658 0 50177 0.1 49861 0.2 49947 0.3 50157 0.4 50414 0.5 50006 0.6 50038 0.7 49962 0.8 50071 0.9 49695Copy the code

For analogy, we reproduce a million Gauss pseudorandom numbers:

Random random = new Random(); DoubleStream gaussianStream = Stream.generate(random::nextGaussian).mapToDouble(e -> e); LinkedHashMap<Range, Integer> gaussianRangeCountMap = gaussianstream.filter (e -> (e >= -1.0&&e < 1.0)).limit(1000000).boxed() .map(Ranges::of) .collect(Ranges::emptyRangeCountMap, (m, e) -> m.put(e, m.get(e) + 1), Ranges::mergeRangeCountMaps); gaussianRangeCountMap.forEach((k, v) -> System.out.println(k.from() +"\t" + v)); 
Copy the code

The output of the code above is exactly what we expect: a double pseudorandom number is evenly distributed, while a Gaussian pseudorandom number of double should be normally distributed.

Results obtained using pseudo-random numbers:

Results obtained using gaussian pseudorandom numbers:

Ps: The full code can be obtained here


Welcome to Zhihu columnKeeping up with Java8, share excellent Java8 Chinese guides and tutorials, and welcome to contribute high-quality articles.

Original link:
javacodegeeks


Translation:
ImportNew.com –
Wild goose found flowers


Translation link:
www.importnew.com/9672.html