Thursday, October 23, 2025
HomeLanguagesJavaRandom setSeed() method in Java with Examples

Random setSeed() method in Java with Examples

The setSeed() method of Random class sets the seed of the random number generator using a single long seed.

Syntax:

public void setSeed() 

Parameters: The function accepts a single parameter seed which is the initial seed.

Return Value: This method has no return value.

Exception: The function does not throws any exception.

Program below demonstrates the above mentioned function:

Program 1:




// program to demonstrate the
// function java.util.Random.setSeed()
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // create random object
        Random r = new Random();
  
        // return the next pseudorandom integer value
        System.out.println("Random Integer value : "
                           + r.nextInt());
  
        // setting seed
        long s = 24;
  
        r.setSeed(s);
  
        // value after setting seed
        System.out.println("Random Integer value : " + r.nextInt());
    }
}


Output:

Random Integer value : -2053473769
Random Integer value : -1152406585

Program 2:




// program to demonstrate the
// function java.util.Random.setSeed()
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // create random object
        Random r = new Random();
  
        // return the next pseudorandom integer value
        System.out.println("Random Integer value : "
                           + r.nextInt());
  
        // setting seed
        long s = 29;
  
        r.setSeed(s);
  
        // value after setting seed
        System.out.println("Random Integer value : "
                           + r.nextInt());
    }
}


Output:

Random Integer value : -388369680
Random Integer value : -1154330330
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS