The uniform random number generator can be used to obtain random numbers with
non-uniform probability distributions. This can be accomplished by inverting the
indefinite integral of the desired distribution (see lecture notes).
As an example, generate energy values distributed according to the
Boltzmann distribution, at a temperature of 298 K, and plot the
distribution as a histogram.
This can be done in the following way.
If you have exited Mathematica since loading the graphics package, type
 | <<Graphics`Graphics` |
The histogram can be generated with the following commands
(Note: here we need the inverse exponential function
which is the natural logarithm, Log[])
 | temp=298.0;
energy := - 0.0083145 temp Log[Random[]];
npoints=5000;
randoms=Table[energy,{n,1,npoints}];
nbins=20;
scalefc=nbins/Max[randoms];
bins=Table[Round[randoms[[i]]*scalefc+0.5],{i,1,npoints}];
counts=Table[Count[bins,i],{i,1,nbins}];
BarChart[counts, PlotRange -> All]; |
Use predefined
mathematica functions to calculate the mean, variance and standard deviation
of your data.
First load the statistical package
 | <<Statistics`DescriptiveStatistics` |
Use Mean[ ... ], Variance[...] and StandardDeviation[...]
commands to analyze the list of your data points.
Show algebraically that this is the right algorithm for
generating the Boltzmann distribution and verify that the generated
data (energy values) are Boltzmann distributed.
true cm
To generate velocity components, for example
, distributed according to
Maxwell distribution, you need to invert the error function. This can be done
in Mathematica by loading in a statistics package (the error function often
shows up in statistical analysis because of its close relationship to
the normal distribution). Be sure to first clear your variables.
Also, start with a smaller number of points, for example npoints=500.
For room temperature and mass of 1, the algorithm is
 | <<Statistics`InverseStatisticalFunctions`;
temp=298.0;
mass=1.0;
vx := (0.75 temp / mass ) * InverseErf[Random[Real, {-1.,1.}]] |
Calculate the average and standard deviation for
both using your random
number distribution and using the analytical solution for a couple of
temperatures, say 300 K and 1000 K. What fraction of the atoms have Vx
within
standard deviation from the mean?
Hannes Jonsson
Modified by Thomas L. Marchioro II
and the Undergraduate Computational Engineering and Science project