Generating Random Numbers in Java

Random Numbers are one of most important concepts in programming paradigm. They are used in various fields ranging from generating encryption keys to simulation. They have also been used heavily on gaming platform, electronics music. The random numbers generated in computers are not random numbers, because they are not random at all and they are generated by very complex algorithm.

Here’s an easy way to generate random numbers in Java.

You could start by importing a library

import java.util.Random;
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);

Then you can use the Random object to get various facilities provided by java.util.Random.

There’s another easy way to generate Random numbers by using Math.random which generates doubles between 0 and 1.