Skip to content

Instantly share code, notes, and snippets.

@vigack
Created January 12, 2018 08:54
Show Gist options
  • Save vigack/7c068fd201cec7d4d45275e4b243f259 to your computer and use it in GitHub Desktop.
Save vigack/7c068fd201cec7d4d45275e4b243f259 to your computer and use it in GitHub Desktop.
Fuck java random
public class Psychic {
private static FuckRandom fuckRandom = new FuckRandom();
static {
fuckRandom.reverse(Math.random());
}
public static double guess(){
return fuckRandom.nextDouble();
}
public static void main(String[] args) {
System.out.println(Psychic.guess());
System.out.println(Math.random());
}
}
class FuckRandom extends Random{
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
public void reverse(double prev){
// return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
long numeric = (long) (prev * (1L << 53));
int old26 = (int) (numeric >>> 27);
int new27 = (int)(numeric & ((1L << 27) - 1));
long oldSeed26 = ((long) old26 << (48 - 26)) & mask;
long newSeed27 = ((long) new27 << (48 - 27)) & mask;
// BruteForce the old seed
for(long oldSeed = oldSeed26; oldSeed<=(oldSeed26|((1L << (48-26)))); oldSeed++){
long newSeed = (oldSeed * multiplier + addend) & mask;
if((newSeed & ((1L << 27) - 1) << (48 - 27)) == newSeed27){
setSeed(newSeed^multiplier);
}
}
}
}
@vigack
Copy link
Author

vigack commented Jan 12, 2018

More fucker solution is to modify Random class use reflect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment