本文仅作为个人学习笔记,记载下来方便以后学习查询~.~
头文件中需包含
#include <curand_kernel.h>
实例代码:
__global__ void generateRandom(long rand)
{
curandState state;
int id = threadIdx.x;
long seed = rand;
curand_init(seed, id, 0, &state);
printf("random double: %f \n",abs(curand_uniform_double(&state)));
}
int main()
{
srand((unsigned int)time(NULL));
cudaSetDevice(0);
generateRandom<<<1,16>>>(rand());
cudaDeviceReset();
return 0;
}
关于cuRAND生成随机数的详细内容,可参考官方文档http://docs.nvidia.com/cuda/curand/device-api-overview.html#axzz4g04UXoC0,其中有详细的例子解释。
生成随机数的函数例子:
curandState localState;
unsigned int x = curand(&localState);
float x = curand_uniform(&localState);
float2 x = curand_normal2(&localState);
double x = curand_uniform_double(&localState);
double x = curand_normal_double(&localState);
另cuda随机数的生成 API 为:
- __device__ void
- curand_init (
- unsigned long long seed, unsigned long long sequence,
- unsigned long long offset, curandState_t *state)