How to generate random variables that are exponentially distributed with average 1/lambda (in C++) ?? is it correct?

Viewed 93

please I need some help.. I need a function in C++ that generates random numbers in exponential distribution with rate lambda per unit time, that is :
f(t)= lambda * exp (- lambda * t) , where t>=0

second question: how can I make all these generated numbers from 0 to 1500 for example ??

here is what I have tried to use..

thank you in advance ..

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
using namespace std;

double ran_expo(double lambda) {
double u;
u = rand() / (RAND_MAX + 1.0);    
return -log(1 - u) / lambda;     // is this correct?? 
}

int main() {
double lambda = 0.0002; const int N = 500; double tT[N];
srand((unsigned)time(NULL));
for (int i = 0; i < N; i++) {
tT[i] = ran_expo(lambda);
}
for (int i = 0; i < N; i++) {
tT[i] = tT[i] + ran_expo(lambda);
cout << "tT=" << tT[i] << "\n";
}
}
0 Answers
Related