我正在尝试生成属于指数分布的随机值。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x;
double random;
double one=1.0;
for(int i=0; i<1000; i++){
x = ((double) rand() / (RAND_MAX));
cout << random <<endl;
x=log(one-random)/(-3);
cout<< x<< endl;
cout<< "__"<<endl;
}
return 0;
}
我正在使用这个公式 x = log(1-u)/(−λ)
我在这篇文章中读到了 Pseudorandom Number Generator - Exponential Distribution
但我看到的输出是这个:
3.90272e-319
0
__
3.90272e-319
0
__
3.90272e-319
0
__
3.90272e-319
0
__
and so on
它总是打印相同的随机数值,而且对数的结果总是0,我无法理解。
最佳答案
您正在使用未初始化的 random
,因此未定义它将持有什么值。而且您永远不会更改它的值,因此循环的每次迭代都将表现相同。
关于c++ - 生成属于指数分布的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29244605/