Created
May 26, 2014 12:23
-
-
Save DarkSeraphim/d8f460d0839be5b8a9f5 to your computer and use it in GitHub Desktop.
Random with preview functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.darkseraphim.util; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Random; | |
/** | |
* @Author DarkSeraphim | |
*/ | |
public class ClairvoyantRandom extends Random | |
{ | |
private Map<Integer, Integer> clairvoyance = new HashMap<Integer, Integer>(); | |
public int previewNextInt(int n) | |
{ | |
int i = super.nextInt(n); | |
clairvoyance.put(n, i); | |
return i; | |
} | |
@Override | |
public int nextInt(int n) | |
{ | |
Integer i = clairvoyance.remove(n); | |
return i != null ? i : super.nextInt(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment