/* 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; & CC Bordeianu, Univ Bucharest, 2007 Support by National Science Foundation */ // decay.c: Spontaneous radioactive decay simulation #include #include // if you don't have drand48 uncomment the following two lines // #define drand48 1.0/RAND_MAX*rand // #define srand48 srand #define lambda 0.01 // the decay constant #define max 1000 // number of atoms at t=0 #define time_max 500 // time range #define seed 68111 // seed for number generator main() { int atom, time, number, nloop; double decay; FILE *output; // save data in decay.dat output = fopen("decay.dat","w"); number = nloop = max; // initial value // seed number generator srand48(seed); // time loop for (time=0; time<=time_max; time++) { // atom loop for (atom=1; atom<=number; atom++) { decay = drand48(); if (decay < lambda) nloop--; // an atom decays } number = nloop; fprintf(output, "%d\t%f\n", time, (double)number/max); } printf("data stored in decay.dat\n"); fclose(output); }