-
-
Save anonymous/5c55ac6e35ceef10136b to your computer and use it in GitHub Desktop.
Pats unicorns predictably by removing unpredictability from the universe
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
import java.lang.Exception; | |
import java.lang.Math; | |
import java.lang.RuntimeException; | |
import java.lang.StackTraceElement; | |
import java.lang.System; | |
import java.util.Arrays; | |
import java.util.HashSet; | |
import java.util.Random; | |
import java.util.Set; | |
public class MagicalLand { | |
public static void main(String[] args) { | |
for (int i = 0; i < (Math.random() * 500) + 2; i++) { | |
if (Unicorn.pat()) { | |
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE"); | |
} | |
} | |
for (int i = 0; i < (Math.random() * 500) + 2; i++) { | |
if (Unicorn.pat()) { | |
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE"); | |
} | |
} | |
System.out.println("END OF PROGRAM"); | |
} | |
} | |
class Unicorn { | |
private static int nextRandom(long seed, int bits) { | |
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); | |
return (int)(seed >>> (48 - bits)); | |
} | |
private static Random getMathematicalRandomness() { | |
double r = Math.random(); | |
long v = (long)(r * (1L << 53)); | |
long c1 = (long)(v >> 27); | |
int c2 = (int)(v & ((1L << 27) - 1)); | |
for (int i=0;i<(1<<22);i++) { | |
long guessSeed = (c1 << 22) + i; | |
int gb = nextRandom(guessSeed, 27); | |
if (gb == c2) { | |
Random fancyRandom = new Random(); | |
fancyRandom.setSeed(guessSeed ^ 0x5DEECE66DL); //Random xors the seed when setting it, so we need to double-xor it | |
fancyRandom.nextInt(); | |
return fancyRandom; | |
} | |
} | |
throw new RuntimeException("Unable to break Math.random(). Is the universe really that random?"); | |
} | |
private static int state = 0; | |
/** | |
* Pat the unicorn three times for truthfullness. | |
* Patting the unicorn may collapse the quantum state of the universe | |
*/ | |
public static boolean pat() { | |
Random fancyRandom = getMathematicalRandomness(); | |
while (fancyRandom.nextDouble() * 500 >= 1) { | |
Math.random(); | |
} | |
state = (state + 1)%3; | |
return state == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment