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.
private static int getRandomNumberInRange(int min, int max) {

	Random r = new Random();
	return r.ints(min, (max + 1)).findFirst().getAsInt();


Top articles in this category:
  1. Given a collection of 1 million integers, All ranging between 1 to 9, how would you sort them in Big O(n) time
  2. Diamond Problem of Inheritance in Java 8
  3. can we write a java method that swaps two integers
  4. Java 8 Parallel Stream with ThreadPool
  5. What is ThreadLocal in Java, where will you use this class
  6. How will you calculate factorial of a large number in Java
  7. Find missing numbers in 4 billion unique numbers with 50MB RAM

Find more on this topic:
Core Java image
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:

This website uses cookies to ensure you get the best experience on our website. more info