生成符合二项分布(binomial distribution)的随机数向量。
int viRngBinomial (const int method, VSLStreamStatePtr stream, const SizeType n, int *r, const int m, const double p);
参数名 |
类型 |
描述 |
输入/输出 |
---|---|---|---|
method |
int |
随机数生成模式。
|
输入 |
stream |
VSLStreamStatePtr类型 |
随机数流。 |
输入 |
n |
SizeType |
输入输出的长度。 |
输入 |
r |
整型指针 |
指向结果的指针。 |
输出 |
m |
int |
二项分布的试验次数。 |
输入 |
p |
double |
二项分布的成功概率。 |
输入 |
#include "krng.h"
#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; float *r = (float *)malloc(sizeof(float) * 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; }