Java random number: methods to generate it

Java language programming is the powerful programming language. Thanks to this programming language, developer can build apps or websites with ease. Meanwhile, in the developing applications process, we often create random numbers. Additionally, many programs have the ability to generate random numbers, such as the OTP. Because it is used to validate the user. Moreover, dice is the best example of random numbers. Because we get a random number between 1 and 6 when we throw it. Therefore, in this blog, ArrowHiTech will introduce you to what Java random number is and how to get random number in java. So let’s get started right now. 

Before coming to how to get random number in Java, we will show you the definition of Java random number

What is Java random number? 

Java random number is that selected from a huge group of numbers using a mathematical algorithm. Moreover, it meets the following two requirements:

  • Firstly, the numbers were evenly distributed over a specific time.
  • Secondly, it’s hard to predict the future value based on current and previous data

That is all about the definition of Java random number. And so now let’s move on How to get random number in Java?

How to get random number in Java?

Using the Math.random() Method

Firstly, the Java Math class contains numerous methods for performing various mathematical operations. Meanwhile, the random() method is one of them. Because, it belongs to the Math class and is a static method. Therefore, it is possible to call it directly. However, only double type random numbers larger than or equal to 0.0 and less than 1.0 are generated. Thus, we must first import the java.lang.Math class before we can use the random() method.

Follow the steps below to create Java random number within a range using Math.random():

  • Firstly, you have to find out the range’s minimum value.
  • Secondly, you have to find out the range’s maximum value.
  • Finally, to create numbers with the min and max values included, you have to use the formula Math.floor(Math.random()*(max-min+1)+min).

NOTE: Only use this method if you need an number or float random value.

For example:

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);

    }

}

Output:

Java random number

Using random class

The Java Random class from the java.util package is another method to create a random number. It creates numbers in a stream. Furthermore, any data type, such as integer, float, double, Boolean, and long, can be used to create a random number. Follow the steps below in case you want to use this class to create Java random number:

  1. Firstly, you have to import the java.util.Random class.
  2. Secondly, you have to create a new class Random by typing Random rand = new. Random()
  3. Finally, you have to choose one of the following rand object methods:
  • nextInt(upperbound) returns a random number between 0 and upperbound-1.
  • Float() returns a float with a value between 0.0 and 1.0.
  • next
  • Double() returns a double with a value between 0.0 and 1.0.

For example:

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);

    }

}

Output:

Java random number

Using ThreadLocalRandom

Follow the steps below to create Java random number with ThreadLocalRandom:

  1. You have to import the java.util.concurrent class. ThreadLocalRandom
  2. You have to use the method.
  • Firstly, ThreadLocalRandom.current().nextInt() is used to create random numbers of type int ().
  • Secondly, nextThreadLocalRandom.urrent uses Int() to generate a random number of type double ().
  • Finally, nextDouble()ThreadLocalRandom.current().nextBoolean is used to create random numbers of type boolean ()

For example:

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); 

    }

}

Output:

Java random number

Conclusion

As we mentioned above about the definition of Java random number and How to get random number in Java?

However, you are still confused about more information about Java. So don’t hesitate. Let’s visit our Java framework development services to let us know your problems or needs. Thank you for reading our blogs.

Tags

Share