我要评分
获取效率
正确性
完整性
易理解

Binomial Distribution Function

Generate a random number vector that conforms to binomial distribution.

Interface Definition

int viRngBinomial (const int method, VSLStreamStatePtr stream, const SizeType n, int *r, const int m, const double p);

Probability Density Function

Parameters

Parameter

Type

Description

Input/Output

method

int

Random number generation method.

  • VSL_RNG_METHOD_BINOMIAL_FAST: fast method.

Input

stream

VSLStreamStatePtr type

Random number stream.

Input

n

SizeType

Length of the output vector.

Input

r

Integer pointer

Pointer to the result.

Output

m

int

Number of trials in the binomial distribution.

Input

p

double

Probability of success in the binomial distribution.

Input

Dependencies

#include "krng.h"

Examples

#include <stdio.h>
#include <stdlib.h>
#include "krng.h"
int main()
{
   /* initialize stream with given BRNG type and seed */
    VSLStreamStatePtr stream;
    unsigned seed = 42;
    int errcode = vslNewStream(&stream, VSL_BRNG_MCG59, seed);
    if (errcode != VSL_STATUS_OK) {
        fprintf(stderr, "Failure in newstream\n");
        return 0;
    }

    SizeType n = 10;
    int *r = (int *)malloc(sizeof(int) * n);
    if (r == NULL) {
        fprintf(stderr, "Failure in malloc\n");
        return 0;
    }

    if (viRngBinomial(VSL_RNG_METHOD_BINOMIAL_FAST, stream, n, r, 100, 0.5)) {
        fprintf(stderr, "Failure in viRngBinomial\n");
        goto out;
    }

    /* deinitialize the stream */
    errcode = vslDeleteStream(&stream);
    if (errcode != VSL_STATUS_OK) {
        fprintf(stderr, "Failure in deleting stream\n");
        goto out;
    }

out:
    free(r);
    return 0;
}