A Simple way to Generate Random number in Java

By August 15, 2015JAVA

Math.Random class

In Java Programming, Math.Randoms class generates random integers, doubles, longs and so on, in various ranges.

To generate a series of random numbers as a unit, you need to use a single Random object – do not create a new Random object for each new random number.

Following example shows this:

import java.util.Random;

/** Generate 10 random integers in the range 0..99. */

public class RandomInteger 


public static final void main(String... aArgs)

log("Generating 10 random integers in range 0..99.");
//note a single Random object is reused here Random randomObj = new Random();
for (int idx = 1; idx <= 10; ++idx)

int randomInt = randomGenerator.nextInt(100);
<pre>log("Generated : " + randomInt);</pre>
private static void log(String aMessage)

Pankaj Singh Sugara

Author Pankaj Singh Sugara

Experienced Software Engineer with a demonstrated history of working in the marketing and advertising industry. Skilled in SQL, Web Applications, PHP, WordPress, and Joomla. Strong engineering professional with a B-TECH focused in Information Technology from JCDM College of Engineering Sirsa, Haryana.

More posts by Pankaj Singh Sugara