Java Utility Library

Java Random - setSeed() Method



The java.util.Random.setSeed() method is used to set the seed of this random number generator using a single long seed.

Syntax

public void setSeed(long seed)

Parameters

seed Specify the initial seed.

Return Value

Returns void type.

Exception

NA

Example:

In the example below, the java.util.Random.setSeed() method is used to set the seed of the given random number generator.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a random object
    Random rand = new Random();

    //setting seed
    rand.setSeed(10);

    //printing after setting seed
    for(int i = 0; i < 5; i++)
      System.out.println("Next Int Value: " + rand.nextInt()); 
  }
}

One of the possible outcome is given below:

Next Int Value: -1157793070
Next Int Value: 1913984760
Next Int Value: 1107254586
Next Int Value: 1773446580
Next Int Value: 254270492

❮ Java.util - Random