gpt4 book ai didi

c++ - curand, thrust::随机

转载 作者:行者123 更新时间:2023-11-28 08:00:42 25 4
gpt4 key购买 nike

我想在 .cu 格式文件中使用 drand48 获得随机统一数,为什么我总是给我 3.90799e-14 这个值

我的代码在 ran_uniform_test.cu 中

#include <iostream>
int main( int argc, char** argv)
{
std::cout<<drand48()<<"\n";
return 0;
}

最佳答案

你应该调用srand48 (或 seed48,或 lcong48)在使用 drand48 之前:

The srand48(), seed48() and lcong48() are initialisation entry points, one of which should be invoked before either drand48(), lrand48() or mrand48() is called. (Although it is not recommended practice, constant default initialiser values will be supplied automatically if drand48(), lrand48() or mrand48() is called without a prior call to an initialisation entry point.) The erand48(), nrand48() and jrand48() functions do not require an initialisation entry point to be called first.

这样使用:

#include <iostream>
#include <ctime>
#include <cstdlib>

int main(int argc, char *argv[])
{
// A common random seed strategy is to use the current time
srand48(time(NULL));

std::cout << drand48() << std::endl;
std::cout << drand48() << std::endl;
std::cout << drand48() << std::endl;
std::cout << drand48() << std::endl;

return 0;
}

关于c++ - curand, thrust::随机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11567432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com