/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. Support by National Science Foundation */ // Decay.java: Spontaneous decay simulation import java.io.*; import java.util.*; public class Decay { static double lambda = 0.01; // Decay constant static int max = 1000, time_max = 500, seed = 68111; // Params public static void main(String[] argv) throws IOException, FileNotFoundException { int atom, time, number, nloop; double decay; PrintWriter w = new PrintWriter(new FileOutputStream("decay.dat"), true); number = nloop = max; // Initial value Random r = new Random(seed); // Seed random generator // Time loop for ( time = 0; time <= time_max; time++ ) { // Atom loop for ( atom = 1; atom <= number; atom++ ) { decay = r.nextDouble(); if (decay < lambda) nloop--; } // An atom decays number = nloop; w.println( " " + time + " " + (double)number/max); } System.out.println("data stored in decay.dat"); } }