Skip to content

Instantly share code, notes, and snippets.

@RyanNutt
Last active May 10, 2018 16:05
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 RyanNutt/dde9989256281f19360f9ac2ae168a29 to your computer and use it in GitHub Desktop.
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
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