Skip to content

fta2012/ReplicatedRandom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Node (MWC1616)

See README in node folder

Java (LCG)

Given a double generated from nextDouble of a java.util.Random, recreate a Random with the same state. This ReplicatedRandom can then be used to predict the future values of the original Random.

Example:

Random r = new Random();
ReplicatedRandom rr = new ReplicatedRandom();
rr.replicateState(r.nextDouble());
System.out.println(r.nextDouble() == rr.nextDouble()); // True
System.out.println(r.nextDouble() == rr.nextDouble()); // True
System.out.println(r.nextDouble() == rr.nextDouble()); // True

Also works with Math.random() since it uses Random internally:

ReplicatedRandom rr = new ReplicatedRandom();
rr.replicateState(Math.random());
System.out.println(Math.random() == rr.nextDouble()); // True
System.out.println(Math.random() == rr.nextDouble()); // True
System.out.println(Math.random() == rr.nextDouble()); // True

Also works for nextInt() but requires two consecutive values:

Random r = new Random();
ReplicatedRandom rr = new ReplicatedRandom();
rr.replicateState(r.nextInt(), r.nextInt());
System.out.println(r.nextInt() == rr.nextInt()); // True
System.out.println(r.nextInt() == rr.nextInt()); // True
System.out.println(r.nextInt() == rr.nextInt()); // True

See this blog post for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published