---
title: 伯努利分布函数
description: "生成符合伯努利分布（Bernoulli distribution）的随机数向量。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0504.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0504.html
indexId: 476ef68cab56d6481b33dbdd11d7e766ad842812809cea45646c77f5c791fe2461
---
# 伯努利分布函数

生成符合伯努利分布（Bernoulli distribution）的随机数向量。

#### 接口定义

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


#### 概率密度函数


#### 参数

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| method | int | 随机数生成模式。 VSL\_RNG\_METHOD\_BERNOULLI\_ICDF：逆累积分布函数法模式 | 输入 |
| stream | VSLStreamStatePtr类型 | 随机数流。 | 输入 |
| n | SizeType | 输出向量的长度。 | 输入 |
| r | 整型指针 | 指向结果的指针。 | 输出 |
| 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;
int *r = (int *)malloc(sizeof(int) * n);
if (r == NULL) {
fprintf(stderr, "Failure in malloc\n");
return 0;
}
if (viRngBernoulli(VSL_RNG_METHOD_BERNOULLI_ICDF, stream, n, r, 0.5)) {
fprintf(stderr, "Failure in viRngBernoulli\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;
}
```
