You can create a random number I / O stream like this:

Stream<Double> randoms = Stream.generate(Math::random); 

Math::random will produce, as I understand it, a random number between (equal to) 0.0 and 1.0 . How can you set a range, for example, between 20 and 50?

    2 answers 2

    You should not use a generator for this. In Java 8, the object of the Random class has methods for creating threads:

    • doubles() - generates an infinite stream of values ​​in the range [0, 1)
    • doubles(long size) - creates a stream with the specified number of values ​​in the range [0, 1)
    • doubles(double origin, double bound) - creates an endless stream of values ​​in the specified range [origin, bound)
    • doubles(long size, double origin, double bound) - creates a stream with the specified number of values ​​in the specified range

    There are also ints and longs methods for creating streams of appropriate types.

       Stream<Double> randoms = Stream.generate(()->Math.random()*30+20)