Random number generator in java

Let’s learn random number generator in java.

How to generate random numbers in java is one of the common interview question.

Let’s see how to generate random numbers in java using few methods,

Random number generator in java

Math.random java

To begin with, let’s look at Math.random() to generate random double number in java.

Math.random() method returns pseudo random double greater than or equal to 0.0 and less than 1.0.

Also read – java program to delete vowels in a given string

Math.random() method uses Random class internally. Let’s see how to generate random numbers in java,

public class RandomNumberInJava
{ 
   public static void main(String[] args) 
   {
      Random rand = new Random();
      double db1 = rand.nextDouble();
      double db2 = Math.random();
      System.out.println(db1);
      System.out.println(db2);
   }
}



Output:

random number generator in java

java.util.random example

In the below java program we are creating instance of class Random to create random numbers.

Then instance methods of Random class namely java random nextint(), nextFloat(), nextBoolean() etc., are invoked.

import java.util.Random;

public class JavaRandomClass 
{ 
   public static void main(String[] args) 
   {
      Random rand = new Random();
      rand.setSeed(12345);
      System.out.println(rand.nextInt());
      System.out.println(rand.nextDouble());
      System.out.println(Math.random());
      System.out.println(rand.nextFloat());
      // generate random byte array
      byte[] randByte = new byte[5];
      rand.nextBytes(randByte);
      System.out.println(Arrays.toString(randByte));
      System.out.println(rand.nextBoolean());
      System.out.println(rand.nextInt(50));
   }
}



Output:

1553932502
0.5132095356731635
0.37046092552767296
0.9171147
[-114, 121, 69, -43, -119]
false
2


java.util.concurrent.ThreadLocalRandom

ThreadLocalRandom class was introduced in java since 1.7. Now let’s see how to generate random numbers in java of data type boolean, double and integer,

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDemo 
{
   public static void main(String[] args)
   {
      // generating random integers between 0 to 999 
      int randInt1 = ThreadLocalRandom.current().nextInt();
      // display random integers 
      System.out.println("Random Integer: " + randInt1);
      // generating random doubles 
      double randDou1 = ThreadLocalRandom.current().nextDouble();
      // display random doubles 
      System.out.println("Random Double: " + randDou1);
      // generating random booleans
      boolean randBool1 = ThreadLocalRandom.current().nextBoolean();
      // display random booleans 
      System.out.println("Random Boolean: " + randBool1);
   }
}



Output:

Random number integer: -1724832069
Random number double: 0.3748088566976404
Random boolean: false



How to generate numbers within range

Let’s learn how to generate random numbers in java within range,

public class RandomNumberDemo 
{
   public static void main(String[] args) 
   {
      int[] randNumbers = new int[10];
      for(int a = 0; a < randNumbers.length; a++) 
      {
          randNumbers[a] = (int)(Math.random() * 100 + 1);
      }
      System.out.println("Generate random numbers in java within range: " + Arrays.toString(randNumbers));
   }
}



Output:

Generate random numbers in java within range: [68, 21, 24, 26, 1, 79, 24, 81, 61, 76]

How to generate 10 numbers

Here let’s learn how to generate 10 random numbers in java. To generate 10 random numbers in java we have to call nextInt() method of class Random,

import java.util.Random;

public class RandomNumberExample 
{
   public static void main(String[] args) 
   {
      Random rd = new Random();
      for(int a = 0; a < 10; a++)
      {
         int randNumber = rd.nextInt(50);
         System.out.println(randNumber); 
      }
   }
}



Output:

44
7
21
39
46
12
18
17
49
32

Generate 5 digit

Let’s learn to generate 5 digit random number in java. Here we are generating 10 random 5 digit number in java,

public class RandomDemo 
{
   public static void main(String[] args) 
   {
      for(int a = 0; a < 10; a++) 
      {
         System.out.println(Math.round(Math.random() * 89999) + 100000);
      }
   }
}



Output:

162819
155592
185233
109780
177746
154535
115534
174557
145013
167943

Generate unique between 1 and 100

Now let’s learn java generate unique random number between 1 and 100. Here’s how to generate random number between 1 & 100,

import java.util.HashSet;
import java.util.Random;

public class RandomExample 
{
   public static void main(String[] args) 
   {
      Random rand = new Random();
      int num;
      int r = 10;
      HashSet<Integer> randNumbers = new HashSet<Integer>();
      for(int a = 0; a < r; a++) 
      {
         num = rand.nextInt(100);
         randNumbers.add(num);
         if(randNumbers.size() <= 10) 
         {
            if(randNumbers.size() == 10) 
            {
               r = 10;
            }
            r++;
            randNumbers.add(num);
         }
      }
      System.out.println("unique random number between 1 and 100: " + randNumbers);
   }
}



Output:

unique random number between 1 and 100: [80, 48, 64, 20, 84, 85, 23, 39, 60, 46, 15]