Last active
May 10, 2018 16:05
-
-
Save RyanNutt/dde9989256281f19360f9ac2ae168a29 to your computer and use it in GitHub Desktop.
Monkeys banging on a keyboard to solve Hello World - https://clsc.be/21
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
public class MonkeyHelloWorld { | |
public static void main( String[] args ) { | |
String target = "Hello World"; | |
String discovered = ""; | |
long counted = 0l; | |
do { | |
counted++; | |
discovered = randomString(); | |
if (counted % 1000 == 0) System.out.println( counted ); | |
} while (!target.equals(discovered)); | |
System.out.println( "Found it in " + counted + " trieds" ); | |
} | |
private static String randomString() { | |
//int len = (int)(Math.random() * 100); | |
int len = 11; | |
//String[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ".split(""); | |
String[] chars = "HeloWrld ".split(""); | |
String out = ""; | |
do { | |
out += chars[(int)(Math.random() * chars.length)]; | |
} while (out.length() < len); | |
return out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment