chance
Java program. Tries to disprove the "Second Law of Thermodynamics = God" argument by finding ordered chaos. See the discussion for more of an explanation.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
/*
* Chance.java
*
* Version:
* $Id: Chance.java,v 1.5 2007/09/25 19:06:07 mwn4938 Exp $
*
* Revisions:
* $Log: Chance.java,v $
* Revision 1.5 2007/09/25 19:06:07 mwn4938
* fixed a spelling error
*
* Revision 1.4 2006/08/01 03:23:44 mwn4938
* minor timing errors fixed...added comments
*
* Revision 1.3 2006/07/09 00:05:14 mwn4938
* changed so that it doesnt print out as it goes. stops on enter or success.
* on enter, prints out current count.
*
* Revision 1.2 2006/05/04 18:41:09 mwn4938
* another attempt with someone else's "console" code...didn't work
*
* Revision 1.1 2006/05/03 21:51:57 mwn4938
* wow this is slow
*
*/
/**
* randomly generates 30 numbers from 0-29 to see if they can ever be generated
* in counting order. will stop on each set once a number is out of order (ex:
* "0", "1", "4"--stop..new set..."2"--stop etc.).
*
* @author Mike Neurohr (mwn4938@rit.edu)
*/
public class Chance {
static long count = 0;// keeps track of the number of sets of numbers
// generated
/**
* the main program
*
* @param args
* (ignored)
*/
public static void main(String[] args) {
new Thread() {
/**
* run on start(). will stop the program (improperly) on enter
*/
public void run() {
BufferedReader buff = new BufferedReader(new InputStreamReader(
System.in));
try {
buff.readLine();
System.out.println(count);// prints the number of
// sets generated
System.exit(0);
} catch (IOException e) {
e.printStackTrace(); // should never happen
}
}
}.start();
boolean stop = false;// boolean for if the program
// should stop (on ordered set)
int nums[] = new int[30];// holds the set
Random gen = new Random();// generates the numbers
while (!stop) {// while the set is not ordered
stop = true;// assume the set is ordered
for (int a = 0; a < 30 && stop; a++) {
nums[a] = gen.nextInt(30);// generate the next number
if (a != nums[a])
stop = false;// if that number isn't in order, stop
}
count++;// increment the count
}
System.out.println(nums + "\n" + count);// print the ordered set
System.exit(0);
}
}