Chapter 12: Random Number Generation > random_uniform

random_uniform

Generates pseudorandom numbers from a uniform (0, 1) distribution.

Synopsis

#include <imsls.h>

float *imsls_f_random_uniform (int n_random, , 0)

The type double function is imsls_d_random_uniform.

Required Arguments

int n_random   (Input)
Number of random numbers to generate.

Return Value

An array of length n_random containing the random uniform (0, 1) deviates.

Synopsis with Optional Arguments

#include <imsls.h>

float *imsls_f_random_uniform (int n_random,
IMSLS_RETURN_USER, float r[],
0)

Optional Arguments

IMSLS_RETURN_USER, float r[]   (Output)
User-supplied array of length n_random containing the random uniform (0, 1) deviates.

Description

Function imsls_f_random_uniform generates pseudorandom numbers from a uniform (0, 1) distribution using a multiplicative congruential method. The form of the generator is as follows:

xi cxi1mod (231 1)

Each xi 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 imsls_random_option. The choice of 16807 will result in the fastest execution time. If no selection is made explicitly, the functions use the multiplier 16807.

Function imsls_random_seed_set can be used to initialize the seed of the random number generator; function imsls_random_option 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 xi from the simple generator, the low-order bits of xi 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 xi, after being scaled into the unit interval, is inserted into the j-th position in the table.

The values returned by imsls_f_random_uniform 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 imsls_f_random_uniform. The following statements (in single precision) would yield random deviates from a uniform (a, b) distribution.

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

Example

In this example, imsls_f_random_uniform generates five pseudorandom uniform numbers. Since function imsls_random_option is not called, the generator used is a simple multiplicative congruential one with a multiplier of 16807.

#include <imsls.h>

#include <stdio.h>

 

#define N_RANDOM  5

 

int main()

{

    float     *r;

 

    imsls_random_seed_set(123457);

 

    r = imsls_f_random_uniform(N_RANDOM, 0);

 

    printf("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


RW_logo.jpg
Contact Support