How can I give java.util.Random a specific seed in thirdparty classes?

By : adum

I have a Java program that loads thirdparty class files (classes I did not write) and executes them. These classes often use java.util.Random, which by default generates random starting seed values every time it gets instantiated. For reasons of reproducability, I want to give these classes the same starting seed every time, changing it only at my discretion.

Here are some of the obvious solutions, and why they don't work:

  1. Use a different Random class in the thirdparty classfiles. The problem here is I only load the class files, and cannot modify the source.

  2. Use a custom classloader to load our own Random class instead of the JVM's version. This approach will not work because Java does not allow classloaders to override classes in the java package.

  3. Swap out the rt.jar's java.util.Random implementation for our own, or putting files into trusted locations for the JVM. These approaches require the user of the application messing with the JVM install on their machine, and are no good.

  4. Adding a custom java.util.Random class to the bootclasspath. While this would technically work, for this particular application, it is impractical because this application is intended for end users to run from an IDE. I want to make running the app convenient for users, which means forcing them to set their bootclasspath is a pain. I can't hide this in a script, because it's intended to be run from an IDE like Eclipse (for easy debugging.)

So how can I do this?

By : adum


Yes option 2 is working: created two classes for testing purpose named and

created jar from ThirdPartyClass.class

jar -cvf tpc.jar ThirdPartyClass.class

created jar from Random.class

jar -cvf rt123.jar Random.class

after that execute with following command:

java  -Xbootclasspath/p:tcp.jar:rt123.jar -cp . -verbose ThirdPartyClass

The output will be: seed value for ThirdPartyClass-> 1

source code>

import java.util.Random;

public class ThirdPartyClass {
    ThirdPartyClass(long seed ) {
        System.out.println("seed value for ThirdPartyClass-> "+seed);

    public static void main(String [] args) {
        ThirdPartyClass tpc=new ThirdPartyClass(new Random().nextLong());

source code>

package java.util;


public class Random extends Object implements Serializable
    public Random() {

    public Random(long seed) {

    public long nextLong() {
        return 1;

Thanks Mahaveer Prasad Mali

By : mahaveer

Consider modifying the third party libraries to have them use a seen for their Random instances. Though you do not have the source code, you can probably edit the bytecode to do it. One helpful toolkit for doing such is ASM.

By : Dave L.

For interoperability we have always used Xml Serialisation and made sure our class was designed from the ground up to do it correctly.

We create an XSD schema document and generate a set of classes from that using XSD.exe. This generates partial classes so we then create a set of corresponding partial classes to add the extra methods we want to help us populate the classes and use them in our application (as they are focused on serialising and deserialising and are a bit difficut to use sometimes).

By : Brody

This video can help you solving your question :)
By: admin