Chapter 12: Random Number Generation > random_general_continuous

random_general_continuous

     

Generates pseudorandom numbers from a general continuous distribution.

Synopsis

#include <imsls.h>

float *imsls_f_random_general_continuous (int n_random, int ndata, float table[],..., 0)

The type double function is imsls_d_random_general_continuous.

Required Arguments

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

int ndata   (Input)
Number of points at which the CDF is evaluated for interpolation. ndata must be greater than or equal to 4.

float *table   (Input/Ouput)
ndata by 5 table to be used for interpolation of the cumulative distribution function.
The first column of
table contains abscissas of the cumulative distribution function in ascending order, the second column contains the values of the CDF (which must be strictly increasing beginning with 0.0 and ending at 1.0) and the remaining columns contain values used in interpolation. This table is set up using function imsls_f_continous_table_setup.

Return Value

An array of length n_random containing the random discrete deviates. To release this space, use free.

Synopsis with Optional Arguments

#include <imsls.h>

float *imsls_f_random_general_continuous (int n_random, int ndata, float table[],
IMSLS_TABLE_COL_DIM, int table_col_dim,
IMSLS_RETURN_USER, float r[],
 0)

Optional Arguments

IMSLS_TABLE_COL_DIM, int table_col_dim   (Intput)
Column dimension of the matrix table.
Default: table_col_dim = 5 

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

Description

Function imsls_f_random_general_continuous generates pseudorandom numbers from a continuous distribution using the inverse CDF technique, by interpolation of points of the distribution function given in table, which is set up by function imsls_f_continuous_table_setup. A strictly monotone increasing distribution function is assumed. The interpolation is by an algorithm attributable to Akima (1970), using piecewise cubics. The use of this technique for generation of random numbers is due to Guerra, Tapia, and Thompson (1976), who give a description of the algorithm and accuracy comparisons between this method and linear interpolation. The relative errors using the Akima interpolation are generally considered very good.

Example 1

In this example, imsls_f_continuous_table_setup is used to set up a table for generation of beta pseudorandom deviates. The CDF for this distribution is computed by the function imsls_f_beta_cdf (Chapter 11, Probability Distribution Functions and Inverses;). The table contains 100 points at which the CDF is evaluated and that are used for interpolation.

 

#include <stdio.h>

#include <imsls.h>

 

float cdf(float);

int main()

{

  int i, iopt=0, ndata= 100;

  float table[100][5], x = 0.0, *r;

 

  for (i=0;i<ndata;i++) {

    table[i][0] = x;

    x += .01;

  }

 

  imsls_f_continuous_table_setup(cdf, iopt, ndata, (float*)table);

 

  imsls_random_seed_set(123457);

  r = imsls_f_random_general_continuous (5,  ndata, table, 0);

  imsls_f_write_matrix("Beta (3, 2) random deviates", 5, 1, r, 0);

 

}

 

float cdf(float x)

{

  return imsls_f_beta_cdf(x, 3., 2.);

}

Output

*** WARNING  Error  from imsls_f_continuous_table_setup.  The values of the

***          CDF in the second column of table did not begin at 0.0 and end

***          at 1.0, but they have been adjusted. Prior to adjustment,

***          table[0][1] = 0.000000e+00 and table[ndata-1][1]= 9.994079e-01.

 

Beta (3, 2) random deviates

       1      0.9208

       2      0.4641

       3      0.7668

       4      0.6536

       5      0.8171


RW_logo.jpg
Contact Support