a shot of dev knowledge

RELATED TAGS

How to generate random numbers in Java

Random numbers within a specific range of type integer, float, double, long, and boolean can be generated in Java.

There are three methods to generate random numbers in Java.

Method 1: Using random class

To use the Random Class to generate random numbers, follow the steps below:

  1. Import the class java.util.Random
  2. Make the instance of the class Random, i.e., Random rand = new Random()
  3. Invoke one of the following methods of rand object:
    • nextInt(upperbound) generates random numbers in the range 0 to upperbound-1.
    • nextFloat() generates a float between 0.0 and 1.0.
    • nextDouble() generates a double between 0.0 and 1.0.
import java.util.Random;
class GenerateRandom {
    public static void main( String args[] ) {
      Random rand = new Random(); //instance of random class
      int upperbound = 25;
        //generate random values from 0-24
      int int_random = rand.nextInt(upperbound); 
      double double_random=rand.nextDouble();
      float float_random=rand.nextFloat();
      
      System.out.println("Random integer value from 0 to" + (upperbound-1) + " : "+ int_random);
      System.out.println("Random float value between 0.0 and 1.0 : "+float_random);
      System.out.println("Random double value between 0.0 and 1.0 : "+double_random);
    }
}

Method 2: Using Math.random

For generating random numbers within a range using Math.random(), follow the steps below:

  1. Declare the minimum value of the range
  2. Declare the maximum value of the range
  3. Use the formula Math.floor(Math.random()*(max-min+1)+min) to generate values with the min and the max value inclusive.

Note: This method can only be used if you need an integer or float random value.

class GenerateRandom {
    public static void main( String args[] ) {
      int min = 50;
      int max = 100;
        
      //Generate random int value from 50 to 100 
      System.out.println("Random value in int from "+min+" to "+max+ ":");
      int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);
      System.out.println(random_int);
    }
}

Method 3: Use ThreadLocalRandom

To generate random numbers using the class ThreadLocalRandom, follow the steps below:

  1. Import the class java.util.concurrent.ThreadLocalRandom
  2. Call the method
    • To generate random number of type int ThreadLocalRandom.current().nextInt()
    • To generate random number of type double ThreadLocalRandom.current().nextDouble()
    • To generate random number of type boolean ThreadLocalRandom.current().nextBoolean()
import java.util.concurrent.ThreadLocalRandom;
class GenerateRandom {
    public static void main( String args[] ) {
      // Generate random integers  
      int int_random = ThreadLocalRandom.current().nextInt();  
  
      // Print random integers 
      System.out.println("Random Integers: " + int_random); 

      // Generate Random doubles 
      double double_rand = ThreadLocalRandom.current().nextDouble(); 
  
      // Print random doubles 
      System.out.println("Random Doubles: " + double_rand); 
       
      // Generate random booleans 
      boolean boolean_rand = ThreadLocalRandom.current().nextBoolean(); 
       
      // Print random booleans 
      System.out.println("Random Booleans: " + boolean_rand); 
    }
}

RELATED TAGS

RELATED COURSES

View all Courses

Keep Exploring