PyXPlot has a range of mathematical functions which draw random samples from a variety of probability distributions. These are:
random_binomial() – returns a random sample from a binomial distribution with
independent trials and a success probability
.
random_chisq() – returns a random sample from a
-squared distribution with
degrees of freedom.
random_gaussian() – returns a random sample from a Gaussian (normal) distribution of standard deviation
and centred upon zero.
random_lognormal() – returns a random sample from the log normal distribution centred on
, and of width
.
random_poisson() – returns a random integer from a Poisson distribution with mean
.
random_tdist() – returns a random sample from a
-distribution with
degrees of freedom.
These functions all rely upon a common underlying random number generator, whose seed may be set using the set seed command, which should be followed by any integer.
Using random numbers to estimate the value of .
PyXPlot’s functions for generating random numbers are most commonly used for adding noise to artificially-generated data. In this example, however, we use them to implement a rather inefficient algorithm for estimating the value of the mathematical constant |
The following script performs this calculation using |
Nsamples = 5000 |
On the author’s machine, this script returns a value of |
With a little modification, this script can be adapted to produce a diagram of the datapoints used in its calculation. Below is a modified version of the second half of the script, which loops over the datapoints stored in the datafile random.dat. It uses PyXPlot’s vector graphics commands, which will be introduced in Chapter 3, to produce such a diagram: |
set multiplot ; set nodisplay |
The graphical output from this script is shown below. The number of datapoints has been reduced to Nsamples |
![]() |