Home » Java programming language

Java Random setSeed() Method with Example

Random Class setSeed() method: Here, we are going to learn about the setSeed() method of Random Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020

Random Class setSeed() method

  • setSeed() method is available in java.util package.
  • setSeed() method is used to set the given seed of this Random Number Generator.
  • setSeed() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • setSeed() method does not throw an exception at the time of set seed.

Syntax:

    public void setSeed(long ss);

Parameter(s):

  • long ss – represents the seed.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void setSeed(long ss) method of 
// Random

import java.util.*;

public class SetSeedOfRandom {
 public static void main(String args[]) {
  // Instantiates Random object
  Random ran = new Random();

  // Display random value before seed
  System.out.println("ran: " + ran.nextInt());

  // By using setSeed(50) method is
  // to set the seed value of this
  // Random Value Generator by the given
  // seed parameter

  ran.setSeed(50);

  // Display random value after seed
  System.out.println("ran.setSeed(50): " + ran.nextInt());
 }
}

Output

RUN 1:
ran: -983822763
ran.setSeed(50): -1160871061

RUN 2:
ran: -460404281
ran.setSeed(50): -1160871061

RUN 3:
ran: 316540002
ran.setSeed(50): -1160871061


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.