Java ThreadLocalRandom longs() methodThe longs() method of Java ThreadLocalRandom class returns an effectively unlimited stream of pseudorandom long values. This method overrides longs in class Random. Syntax:Parameter:NA Returns:This method returns a stream of pseudorandom long values. ExampleTest it NowOutput: stream of pseudorandom long value is: java.util.stream.LongPipeline$Head@136432db Java ThreadLocalRandom longs(long streamSize) methodThe longs(long streamSize) method of Java ThreadLocalRandom class returns a stream producing the given streamSize number of pseudorandom long values. This method overrides longs in class Random. Syntax:Parameter:streamSize - It is the number of values to generate Returns:This method returns a stream of long values. Exception:IllegalArgumentException: This exception will throw if the streamSize is less than zero. Example 1Test it NowOutput: stream of longs value is: java.util.stream.LongPipeline$Head@7382f612 Example 2Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException: size must be non-negative at java.base/java.util.concurrent.ThreadLocalRandom.longs(Unknown Source) at tests.ThreadLocalRandomLongsExample1.main(ThreadLocalRandomLongsExample2.java:7) Java ThreadLocalRandom longs(long randomNumberOrigin, long randomNumberBound) methodThe longs(long randomNumberOrigin, long randomNumberBound) method of Java ThreadLocalRandom class returns an effectively unlimited stream of pseudorandom long values. Each value must conform to the given origin (inclusive) and bound (exclusive). This method overrides longs in class Random. Syntax:Parameter:randomNumberOrigin: It is the origin (inclusive) of each random value. randomNumberBound: It is the bound (exclusive) of each random value. Returns:This method returns a stream of pseudorandom long values. Exception:IllegalArgumentException: This exception will throw if randomNumberOrigin is greater than or equal to randomNumberBound. Example 1Test it NowOutput: stream of pseudorandom long value is: java.util.stream.LongPipeline$Head@136432db Example 2Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException: bound must be greater than origin at java.base/java.util.concurrent.ThreadLocalRandom.longs(Unknown Source) at tests.ThreadLocalRandomLongsExample2.main(ThreadLocalRandomLongsExample2.java:7) Java ThreadLocalRandom longs(long streamSize, long randomNumberOrigin, long randomNumberBound) methodThe longs(long streamSize, long randomNumberOrigin, long randomNumberBound) method of Java ThreadLocalRandom class returns a stream producing the given streamSize number of pseudorandom long values. Each value must conform to the given origin (inclusive) and bound (exclusive). This method overrides longs in class Random. Syntax:Parameter:streamSize - It is the number of values to generate randomNumberOrigin - It is the origin (inclusive) of each random value randomNumberBound - It is the bound (exclusive) of each random value Returns:This method returns a stream of pseudorandom long values. Exception:
Example 1Test it NowOutput: stream of pseudorandom long value is: java.util.stream.LongPipeline$Head@136432db Example 2Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException: size must be non-negative at java.base/java.util.concurrent.ThreadLocalRandom.doubles(Unknown Source) at tests.ThreadLocalRandomLongsExample2.main(ThreadLocalRandomLongsExample1.java:7)
Next TopicJava ThreadLocalRandom
|