How to Change Random Class Seed Value in java


In this example, we can see how to change random class seed value in Java.

Source Code

package com.beginner.examples;

import java.util.Random;

public class ChangeRandomSeed {

	public static void main(String[] args) {
		//Create Random Object
		Random random = new Random(256);
		for(int i=0;i<5;i++){
			System.out.println(random.nextInt(100));
		}
		
		/*
		 * To change the seed use the method of setSeed() of Random class.
		 */
		random.setSeed(512);
		System.out.println("Change random seed :");
		for(int i=0;i<5;i++){
			System.out.println(random.nextInt(100));
		}

	}

}

Output:

19
37
75
50
19
Change random seed :
41
69
84
40
7

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments