Example: Mersenne Twister Random Number Generation

In this example, four simulation streams are generated. The first series is generated with the seed used for initialization. The second series is generated using an array for initialization. The third series is obtained by resetting the generator back to the state it had at the beginning of the second stream. Therefore, the second and third streams are identical. The fourth stream is obtained by resetting the generator back to its original, uninitialized state, and having it reinitialize using the seed. The first and fourth streams are therefore the same.
import com.imsl.stat.*;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class MersenneTwisterEx1 {
    
    public static void main(String args[])  {

        int nr = 4;
        double[] r = new double[nr];
        int s = 123457;
        /* Initialize MersenneTwister with a seed */
        MersenneTwister mt1 = new MersenneTwister(s);
        MersenneTwister mt2 = (MersenneTwister) mt1.clone();
        /* Save the state of MersenneTwister */
        try{
            FileOutputStream fos = new FileOutputStream("mt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(mt1);
            oos.close();
            fos.close();
        } catch (IOException e) {
        }
        Random rndm = new Random(mt1);
        
        /* Get the next five random numbers */
        for (int k=0; k < nr; k++) {
           r[k] = rndm.nextDouble();
        }
        
        System.out.println("              First Stream Output");
        System.out.println(r[0]+"     "+r[1]+"     "+r[2]+"     "+r[3]);
        
        /* Check the cloned copy against the original */
        Random rndm2 = new Random(mt2);
        for (int k=0; k < nr; k++) {
           r[k] = rndm2.nextDouble();
        }
        
        System.out.println("\n              Clone Stream Output");
        System.out.println(r[0]+"     "+r[1]+"     "+r[2]+"     "+r[3]);
        
        /* Check the serialized copy against the original */
        try{
            FileInputStream fis = new FileInputStream("mt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            mt2=(MersenneTwister) ois.readObject();
        } catch (IOException e1){
        } catch (java.lang.ClassNotFoundException e2) {
        }
        Random rndm3 = new Random(mt2);
        for (int k=0; k < nr; k++) {
           r[k] = rndm3.nextDouble();
        }
        System.out.println("\n              Serialized Stream Output");
        System.out.println(r[0]+"     "+r[1]+"     "+r[2]+"     "+r[3]);
    }
}

Output

              First Stream Output
0.43474506366564114     0.013851109283287921     0.49560038426424047     0.7012807898922319

              Clone Stream Output
0.43474506366564114     0.013851109283287921     0.49560038426424047     0.7012807898922319

              Serialized Stream Output
0.43474506366564114     0.013851109283287921     0.49560038426424047     0.7012807898922319
Link to Java source.