Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Created May 26, 2014 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkSeraphim/d8f460d0839be5b8a9f5 to your computer and use it in GitHub Desktop.
Save DarkSeraphim/d8f460d0839be5b8a9f5 to your computer and use it in GitHub Desktop.
Random with preview functionality
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