---
title: 均匀分布函数
description: "生成符合均匀分布（Uniform distribution）的随机数向量。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0492.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0492.html
indexId: 33fc03741909136eb0b7f8a684febbd6969c8ab115986dd9308222f1d7ccb6f361
---
# 均匀分布函数

生成符合均匀分布（Uniform distribution）的随机数向量。

#### 接口定义

int vsRngUniform (const int method, VSLStreamStatePtr stream, const SizeType n, float *r, const float a, const float b);

int vdRngUniform (const int method, VSLStreamStatePtr stream, const SizeType n, double *r, const double a, const double b);

int viRngUniform(const int method, VSLStreamStatePtr stream, const SizeType n, int *r, const int a, const int b);

int viRngUniformBits32(const int method, VSLStreamStatePtr stream, const SizeType n, uint32_t *r);

int viRngUniformBits64(const int method, VSLStreamStatePtr stream, const SizeType n, uint64_t *r);

int viRngUniformBits(const int method, VSLStreamStatePtr stream, const SizeType n, unsigned int *r);


#### 概率密度函数


#### 参数

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| method | int | 随机数生成模式。 VSL\_RNG\_METHOD\_UNIFORM\_STD：标准模式 VSL\_RNG\_METHOD\_UNIFORM\_STD\_ACCURATE：精确模式 | 输入 |
| stream | VSLStreamStatePtr类型 | 随机数流。 | 输入 |
| n | SizeType | 输出向量的长度。 | 输入 |
| r | 在vsRngUniform中是单精度浮点类型指针。 在vdRngUniform中是双精度浮点类型指针。 在viRngUniform中是整型指针。 在viRngUniformBits32中是32位无符号整型指针。 在viRngUniformBits64中是64位无符号整型指针。 在viRngUniformBits中是无符号整型指针。 | 指向结果的指针。 | 输出 |
| a | 在vsRngUniform中是单精度浮点类型。 在vdRngUniform中是双精度浮点类型。 在viRngUniform中是整型。 | 均匀分布的min。 | 输入 |
| b | 在vsRngUniform中是单精度浮点类型。 在vdRngUniform中是双精度浮点类型。 在viRngUniform中是整型。 | 均匀分布的max。 | 输入 |


#### 依赖

#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 (vsRngUniform(VSL_RNG_METHOD_UNIFORM_STD, stream, n, r, 0.0, 1.0)) {
fprintf(stderr, "Failure in vsRngUniform\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;
}
```
