private static int getRandomNumberInRange(int min, int max) {
Random r = new Random();
return r.ints(min, (max + 1)).findFirst().getAsInt();
}
Generate Random Numbers in a range using Java 8
Carvia Tech | December 27, 2017 | 1 min read | 208 views | Java 8
Java 8 Random.ints
In Java 8, new methods are added in java.util.Random
This Random.ints(int origin, int bound) or Random.ints(int min, int max) generates a random integer from origin (inclusive) to bound (exclusive).
3.1 Code snippet.
Top articles in this category:
- Given a collection of 1 million integers, all ranging between 1 to 9, sort them in Big O(n) time
- Secure OTP generation in Java
- Factorial of a large number in Java BigInteger
- Java 8 Parallel Stream custom ThreadPool
- Diamond Problem of Inheritance in Java 8
- can we write a java method that swaps two integers
- Find missing numbers in 4 billion unique numbers with 50MB RAM
Find more on this topic:
Subscribe to Interview Questions
Recommended books for interview preparation:
Book you may be interested in..
Book you may be interested in..
Similar Posts
- Code review checklist for Java developers
- Count word frequency in Java
- Secure OTP generation in Java
- HmacSHA256 Signature in Java
- Submit Form with Java 11 HttpClient - Kotlin
- Java Exception Class Hierarchy
- Http download using Java NIO FileChannel
- CRC32 checksum calculation Java NIO
- Precision and scale for a Double in java
- Difference between HashMap, LinkedHashMap and TreeMap