Java.util.Random class in Java with examples

What is java.util.Random class and its methods with example?. In this blog, we will learn about java.util.Random class Random in detail with examples. In simple terms, this class is used for generating a random number. It also gives the advantage of generating random numbers (or values) of various types like int, double, boolean, long, float, etc.

  • If two instances of Random (long seed) class are created using the same seed, it generates and returns a sequence with the same number.

Constructors

Random(long seed) Creates a new randomized number generator based on a long seed value.

Declaration of Random Class

public class Random extends Object implements Serializable

Random Class Methods with Examples

Some methods of the java.util.Random class.

  • nextInt():  Returns next pseudorandom, from int value of randomized number generator sequence.
import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator.
        Random randomObj = new Random();
        System.out.println("Generated int Value from nextInt() "
                + ": " + randomObj.nextInt());
    }
}
Output
Generated int Value from nextInt() : 460689581

  • nextInt(int bound): Returns next pseudorandom, from int value between 0 and specified value of randomized number generator sequence.
import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator.
        Random randomObj = new Random();
        System.out.println("Generated int Value from nextInt(int bound) "
                + ": " + randomObj.nextInt(15));
    }
}
Output
Generated int Value from nextInt(int bound) : 13

  • nextFloat():  Returns next pseudorandom, from float value between 0.0 and 1.0 of randomized number generator sequence.
import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator.
        Random randomObj = new Random();
        System.out.println("Generated float Value from nextFloat() "
                + ": " + randomObj.nextFloat());
    }
}
Output
Generated float Value from nextFloat() : 0.3447292

  • nextDouble():  Returns next pseudorandom, from double value between 0.0 and 1.0 of randomized number generator sequence.
import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator.
        Random randomObj = new Random();
        System.out.println("Generated double Value from nextDouble() "
                + ": " + randomObj.nextDouble());
    }
}
Output
Generated double Value from nextDouble() : 0.7434493177130183

  • nextBoolean(): Returns next pseudorandom, from boolean value (true or false) of randomized number generator sequence.
import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator.
        Random randomObj = new Random();
        System.out.println("Generated boolean Value from nextBoolean() "
                + ": " + randomObj.nextBoolean());
    }
}
Output
Generated boolean Value from nextBoolean() : true

  • nextLong(): Returns next pseudorandom, from long value of randomized number generator sequence.
import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator.
        Random randomObj = new Random();
        System.out.println("Generated long Value from nextLong() "
                + ": " + randomObj.nextLong());
    }
}
Output
Generated long Value from nextLong() : -3261187088079444735

Example of Random(long seed) Constructor

import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator 
        //with the seed value 200.
        Random randomObj = new Random(200);
        System.out.println("Random(long seed) Constructor---\n"
                + "Generated Value from nextInt() "
                + ": " + randomObj.nextInt());
    }
}
Output
Random(long seed) Constructor---
Generated Value from nextLong() : -1133938638

setSeed(long seed) method with Example

  • setSeed(long seed)With the help of this method, we can change the seed value of random number generator. Also, we can generate the same or identical sequence of random sequence generator.
import java.util.Random;

public class RandomClassExamples {

    public static void main(String[] args) {

        //initializing the random number generator.   
        System.out.println("Using the Random(long seed) Constructor");
        Random randomObj = new Random(35);
        for(int i = 0; i<3;i++){
            System.out.println(randomObj.nextInt());
        }
        System.out.println("\nUsing the setSeed(long seed) method");
        //set the seed value to 35 to generate the same sequencing.
        randomObj.setSeed(35);
        
        for(int i = 0; i<3;i++){
            System.out.println(randomObj.nextInt());
        }
    }
}
Output
Using the Random(long seed) Constructor
-1167411792
736328087
2132076332

Using the setSeed(long seed) method
-1167411792
736328087
2132076332

Also read : A program for generating random string using Java.util.Random

That’s enough for a quick overview of Java Random Class.

Thank you

Leave a Reply

Your email address will not be published.