简体   繁体   中英

Generate infinite DoubleStream

I can generate Stream<Double> using generate . Eg

Stream.generate(Math::random).limit(10).forEach(System.out::println);

How to generate infinte random DoubleStream instead of Stream<Double> ?

You can use Random#doubles to generate DoubleStream

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive). A pseudorandom double value is generated as if it's the result of calling the method nextDouble().

Eg

new Random().doubles().limit(10).forEach(System.out::println);

May be you can try this:

   DoubleStream i = DoubleStream.generate(new Random()::nextDouble);
   Stream<Double> o =  i.boxed();
   o.limit(10).forEach(System.out::println);

You can read more about them here ( https://docs.oracle.com/javase/8/docs/api/java/util/stream/DoubleStream.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM