Pyxplot has functions for generating random numbers from a variety of common probability distributions. These functions are in the random module:
random.random() – returns a random real number between 0 and 1.
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 on 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 on a common underlying random number generator1, whose seed may be set using the set seed command, which should be followed by any integer. The sequence of random samples generated is always the same after setting any particular seed.
When Pyxplot starts, the seed is implicitly set to zero. This means that Pyxplot always produces the same series of random numbers when restarted. This series can be reproduced by typing:
set seed 0
For applications where this repeatability is undesirable, the following command may help, using the system clock as a random seed:
set seed time.now().toUnix()
This gives a different sequence of random numbers each second. However, the user is advised to consider carefully whether this is sufficient for the particular application being implemented.
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 = 500 |
rand() = random.random() |
set samp Nsamples |
n=0 |
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 data points stored in the data file 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 |
# Draw a unit circle and a unit square |
# Now plot the positions of these random data points andi |
The graphical output from this script is shown below. The number of datapoints has been reduced to Nsamples |
![]() |
Footnotes