Generate Random Numbers in a range using Java 8

Carvia Tech | December 27, 2017 at 11:31 PM | 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. Diamond Problem of Inheritance in Java 8
  2. Given a collection of 1 million integers, All ranging between 1 to 9, how would you sort them in Big O(n) time
  3. What is ThreadLocal in Java, where will you use this class
  4. How will you calculate factorial of a large number in Java
  5. How will you increment each element of an Integer array, using parallel operation
  6. Is Java Pure Object Oriented Language?
  7. What is Deadlock in Java? How to troubleshoot and how to avoid deadlock


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 month ago


Recommended books for interview preparation:
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel

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