0

I'm trying to make an Exponential Random number generator in matlab. Life would be easy if I could just use exprand but nope :( this is the function I have I want to see if it's correct.

function x=erv(lambda)%generatae Exponential random variables
x=(-(1/lambda)*log(urv(1)));%using the urv function from 1a 
end

So the URV function give me a random number from [0,1]

function y=urv(howMany) %generate random numbers between [0,1] (uniform random variable), input how many RV's you want
for k=(1:howMany)
    y(:,k)=(2*eps)*round(rand/(2*eps));
end
end

So can anyone give me any insight if URV is correct and if erv is correct. Thanx

JEM
  • 1
  • See [here](http://stackoverflow.com/questions/2106503/pseudorandom-number-generator-exponential-distribution). And why don't you use `rand` directly instead of `urv`? – Luis Mendo Mar 13 '14 at 17:41
  • `rand` will give you uniformly distributed random values between 0 and 1, not need to use URV – MZimmerman6 Mar 13 '14 at 18:48
  • The reason I made URV is because I was asked to make a Uniform Random Variable with [0,1], and I think the rand function only does (0,1) – JEM Mar 13 '14 at 19:53
  • 2
    A uniform RV in [0 1] takes values 0 or 1 with probsability 0. So don't worry about the distinction between [0 1] and (0 1), and use just `rand` – Luis Mendo Mar 13 '14 at 21:49

1 Answers1

0

It is not correct, because your random number generator is unable to generate some numbers, for example x=2.3^-17.

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • The reason I made URV is because I was asked to make a Uniform Random Variable with [0,1], and I think the rand function only does (0,1) . If it's incorrect can you help me fix it? Thanx – JEM Mar 13 '14 at 19:52
  • A bit of explanation substantiating your statement would be helpful. – pjs Mar 13 '14 at 21:55
  • @JEM: I don't know any practical random number generator which generates floating point values in a closed interval and matches all possible double values. @pjs: The double value `x` can't be the result of `(2*eps)*round(...)`, one result was to low and the next was to high without any other possible value in between because of `round` – Daniel Mar 13 '14 at 22:03