randomUniform¶
Generates pseudorandom numbers from a uniform (0, 1) distribution.
Synopsis¶
randomUniform (nRandom)
Required Arguments¶
- int
nRandom
(Input) - Number of random numbers to generate.
Return Value¶
An array of length nRandom
containing the random uniform (0, 1)
deviates.
Description¶
Function randomUniform
generates pseudorandom numbers from a uniform (0,
1) distribution using a multiplicative congruential method. The form of the
generator is as follows:
Each \(x_i\) is then scaled into the unit interval (0, 1). The possible
values for c in the generators are 16807, 397204094, and 950706376. The
selection is made by the function randomOption
. The choice of 16807 will
result in the fastest execution time. If no selection is made explicitly,
the functions use the multiplier 16807.
Function randomSeedSet can be used to initialize the seed of the random number generator; function randomOption can be used to select the form of the generator.
The user can select a shuffled version of these generators. In this scheme, a table is filled with the first 128 uniform (0, 1) numbers resulting from the simple multiplicative congruential generator. Then, for each \(x_i\) from the simple generator, the low-order bits of \(x_i\) are used to select a random integer, j, from 1 to 128. The j-th entry in the table is then delivered as the random number, and \(x_i\), after being scaled into the unit interval, is inserted into the j-th position in the table.
The values returned by randomUniform
are positive and less than 1.0.
However, some values returned may be smaller than the smallest relative
spacing; hence, it may be the case that some value, for example r
[i
], is such that 1.0 − r
[i
] = 1.0.
Deviates from the distribution with uniform density over the interval
\((a,b)\) can be obtained by scaling the output from randomUniform
.
The following statements (in single precision) would yield random deviates
from a uniform \((a,b)\) distribution.
r = randomUniform (nRandom)
for i in range(0, nRandom): r[i] = r[i]*(b-a) + a;
Example¶
In this example, randomUniform
generates five pseudorandom uniform
numbers. Since function randomOption
is not called, the generator used
is a simple multiplicative congruential one with a multiplier of 16807.
from __future__ import print_function
from numpy import *
from pyimsl.stat.randomUniform import randomUniform
from pyimsl.stat.randomSeedSet import randomSeedSet
n_random = 5
randomSeedSet(123457)
r = randomUniform(n_random)
print("Uniform random deviates: %8.4f%8.4f%8.4f%8.4f%8.4f\n"
% (r[0], r[1], r[2], r[3], r[4]))
Output¶
Uniform random deviates: 0.9662 0.2607 0.7663 0.5693 0.8448