Skip to content

Instantly share code, notes, and snippets.

@maghul
Last active December 13, 2015 23:59
Show Gist options
  • Save maghul/4995368 to your computer and use it in GitHub Desktop.
Save maghul/4995368 to your computer and use it in GitHub Desktop.
A solution to the MagicLand puzzle
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");
}
}
public class Math {
// Where the magic is...
public static double random() {
final double r= java.lang.Math.random();
Unicorn.shouldBePatted((int)(r * 500) + 2);
return r;
}
}
public class Unicorn {
private static int count= 0;
public static boolean pat() {
return (++count)==1;
}
public static void shouldBePatted(int times) {
if ( count>=times ) {
count=0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment