Chapter 12: Random Number Generation > random_hypergeometric

random_hypergeometric

Generates pseudorandom numbers from a hypergeometric distribution.

Synopsis

#include <imsls.h>

int *imsls_f_random_hypergeometric (int n_random, int n, int m,
int l, ..., 0)

The type double function is imsls_d_random_hypergeometric.

Required Arguments

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

int n   (Input)
Number of items in the sample. Parameter n must be positive.

int m   (Input)
Number of special items in the population, or lot. Parameter m must be positive.

int l   (Input)
Number of items in the lot. Parameter l must be greater than both n and m.

Return Value

An integer array of length n_random containing the random hypergeometric deviates.

Synopsis with Optional Arguments

#include <imsls.h>

int *imsls_f_random_hypergeometric (int n_random, int n, int m,
int l,
IMSLS_RETURN_USER, int ir[],
0)

Optional Arguments

IMSLS_RETURN_USER, int ir[]   (Output)
User-supplied integer array of length n_random containing the random hypergeomet­ric deviates.

Description

Function imsls_f_random_hypergeometric generates pseudorandom numbers from a hypergeomet­ric distribution with parameters N, M, and L. The hypergeometric random variable X can be thought of as the number of items of a given type in a random sample of size N that is drawn without replacement from a population of size L containing M items of this type. The probabil­ity function is

for x = max (0, N  L + M), 1, 2, , min (NM)

If the hypergeometric probability function with parameters N, M, and L evaluated at N  L + M (or at 0 if this is negative) is greater than the machine epsilon (see imsls_f_machine, Chapter 15, “Utilities”;), and less than 1.0 minus the machine epsilon, then imsls_f_random_hypergeometric uses the inverse CDF technique. The function recursively computes the hypergeometric probabilities, starting at x = max (0, N  L + M) and using the ratio

(see Fishman 1978, p. 475).

If the hypergeometric probability function is too small or too close to 1.0, the imsls_f_random_hyper­geometric generates integer deviates uniformly in the interval [1, L  i] for i = 0, 1, ..., and at the i-th step, if the generated deviate is less than or equal to the number of special items remain­ing in the lot, the occurence of one special item is tallied and the number of remaining special items is decreased by one. This process continues until the sample size of the number of special items in the lot is reached, whichever comes first. This method can be much slower than the inverse CDF technique. The timing depends on N. If N is more than half of L (which in practical examples is rarely the case), the user may wish to modify the problem, replacing N by L  N, and to consider the generated deviates to be the number of special items not included in the sample.

Example

In this example, imsls_f_random_hypergeometric generates five pseudorandom hypergeometric deviates from a hypergeometric distribution to simulate taking random samples of size 4 from a lot containing 20 items, of which 12 are defective. The resulting hypergeometric deviates rep­resent the numbers of defectives in each of the five samples of size 4.

#include <imsls.h>

#include <stdio.h>

 

int main()

{

    int n_random = 5;

    int n = 4;

    int m = 12;

    int l = 20;

    int *ir;

 

    imsls_random_seed_set(123457);

    ir = imsls_f_random_hypergeometric(n_random, n, m, l, 0);

    imsls_i_write_matrix("Hypergeometric random deviates: ",

        1, n_random, ir, IMSLS_NO_COL_LABELS, 0);

}

Output

Hypergeometric random deviates:

        4   2   3   3   3

Fatal Errors

IMSLS_LOT_SIZE_TOO_SMALL                The lot size must be greater than the sample size and the number of defectives in the lot. Lot size = #. Sam­ple size = #. Number of defectives in the lot = #.


RW_logo.jpg
Contact Support