Program
/* spontaneous decay simulation */
#include <stdio.h>
#include <math.h>
#define lambda 0.01 /* the decay constant */
#define max 1000 /* number of atoms at t=0 */
#define time_max 500 /* time range */
#define seed 11168 /* seed for number generator */
FILE *output; /* internal file name */
main()
{
int atom, time, number, nloop;
double decay;
number=nloop=max; /* initial value */
output = fopen("decay.dat", "w"); /* external file name */
srand48(seed); /* seed the number generator */
for (time=0; time<=time_max; time++) /* time loop */
{
for (atom=1; atom<=number; atom++) /* atom loop */
{
decay=drand48();
if (decay<lambda) nloop--; /* an atom decays */
}
number=nloop;
fprintf(output, "%d\t%f\n", time, (double) number/max);
}
fclose (output);
}
A DEC C source and a ANSI-C source
which you can save and run on your computer.
Back to main document.