Uniformly random generator

Please i am trying to solve this problem:
How to generate an exponential r.v. with parameter lambda from U(0,1)? Show
all your calculations. Write a short piece of C or C++ code to generate 1000 exponential r.v. from your equation, using lambda=85. What is the mean and variance of the 1000 random variables you have generated? Do they agree with the expected value and the variance of
an exponential r.v. with lambda=85?

my code so far is below. I want it to display the result in an array and be able to send the output to an excel file. I also noticed that my variance is not correct. can someone help me. the formula i am using is x = - 1/lambda*log(u)

do {
u = rand() / ((float)RAND_MAX+1);
x = (-(log(u)/lambda));
i++;
cout << x << " \n";
sum =sum + x;
mean = (sum/1000);
var = var + pow((x - mean),2);
}
while (i<1000);
Last edited on
Arrays don't display anything. Not only that, but it is very, very, very, very hard (almost impossible) to export to Excel format.
excel can read csv files.

Just output the file in this format and save it as "whatever.csv"

1
2
3
R1 C1,R1 C2,R1 C3
R2 C1,R2 C2
... etc
Topic archived. No new replies allowed.