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

A vector of length nRandom containing the random uniform (0, 1) deviates.

Description

The function randomUniform generates pseudorandom numbers from a uniform (0, 1) distribution using a multiplicative congruential method. The form of the generator is

\[x_i \equiv cx_{i-1} mod (2^{31} − 1)\]

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.

The function randomSeedSet can be used to initialize the seed of the random number generator. The 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. Some values returned may be smaller than the smallest relative spacing, however. 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.

float *r;
r =randomUniform (nRandom, 0);
for (i=0; i<nRandom; i++) r[i]*(b-a) +a;

Example

In this example, randomUniform is used to generate five pseudorandom uniform numbers. Since 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.math.randomSeedSet import randomSeedSet
from pyimsl.math.randomUniform import randomUniform

randomSeedSet(123457)
r = randomUniform(5)
print("Uniform random deviates: %8.4f%8.4f%8.4f%8.4f%8.4f"
      % (r[0], r[1], r[2], r[3], r[4]))

Output

Uniform random deviates:   0.9662  0.2607  0.7663  0.5693  0.8448