简体   繁体   English

生成属于指数分布的随机数

[英]Generate random number belonging to exponential distribution

I'm trying to generate random values belonging to an exponential distribution. 我正在尝试生成属于指数分布的随机值。

#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;
}

I'm using this formula x = log(1-u)/(−λ) that I have read in this post Pseudorandom Number Generator - Exponential Distribution 我正在使用这个公式x = log(1-u)/(−λ) ,我在这篇文章中已经阅读过伪随机数生成器-指数分布

But the output that I see is this one: 但是我看到的输出是这样的:

3.90272e-319
0
__
3.90272e-319
0
__
3.90272e-319
0
__
3.90272e-319
0
__
and so on

It is always printed the same value of the random number and also the result of the logarithm is always 0, I can't understand way. 它总是打印出与随机数相同的值,并且对数的结果始终为0,我无法理解。

You are using random uninitialized, so it is undefined what value it will hold. 您使用的是random未初始化的,因此未定义它将保存什么值。 And you are never changing its value, so each iteration of the loop it will behave the same. 而且您永远不会更改其值,因此循环的每次迭代都将表现相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM