dcsimg
 

How to Generate Random Integers in Java

Tuesday May 18th 2021 by MS Sridhar
Article Image

This Java example code shows you how to generate random streams of integer values. Check out the code.

Generating random integer values in Java is pretty straightforward. In this programming tutorial, we will be using the int method of the Random class from java.util.Random to achieve our goals.

*/

import java.util.Random;

public class RandomIntGenerator {

    public static void main(String[] args)
	{
		RandomIntGenerator randomIntGenerator = new RandomIntGenerator();
		randomIntGenerator.proceed();
	}

	private void proceed()
	{
		Random random = new Random();
		random.ints(100,200).forEach(System.out::println);
	}
}

/*

The output is not complete. It is just a generator and the values are unlimited. Press Ctrl+C to stop the application.

Output:

$java RandomIntGenerator
153
100
133
141
148
121
132
133
136
188
Home
Mobile Site | Full Site