Create two random number generators with the same seed in Java

Description

The following code shows how to create two random number generators with the same seed.

Example


/*  www  . j av  a2 s  .c o  m*/
import java.util.Random;

public class Main {
  public static void main(String[] argv) throws Exception {
    Random rand = new Random();
    long seed = rand.nextLong();
    System.out.println(seed);
    rand = new Random(seed);
    Random rand2 = new Random(seed);
    System.out.println(rand2.nextLong());
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization