```
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 | 170 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, how would you sort them in Big O(n) time
- Diamond Problem of Inheritance in Java 8
- can we write a java method that swaps two integers
- Java 8 Parallel Stream with ThreadPool
- What is ThreadLocal in Java, where will you use this class
- How will you calculate factorial of a large number in Java
- Find missing numbers in 4 billion unique numbers with 50MB RAM

###### Find more on this topic:

##### Core Java

Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.

Last updated 1 week ago

###### Recommended books for interview preparation:

Book you may be interested in..

Book you may be interested in..

Book you may be interested in..

##### Similar Posts

- Submit Form with Java 11 HttpClient - Kotlin
- Explain Java Exception Class Hierarchy
- Http download using Java NIO FileChannel
- CRC32 checksum calculation Java NIO
- Set precision and scale for a double value in java
- Difference between HashMap, LinkedHashMap and TreeMap
- What is difference between ExecutorService submit and execute method
- What is left shift right shift and unsigned rght shift operator in Java
- What happens when wait() & notify() method are called
- can we write a java method that swaps two integers