23 views (last 30 days)

Ameer Hamza
on 1 Apr 2020

Edited: Ameer Hamza
on 1 Apr 2020

An interesting approach is suggested by David, this turn out to be quite easy.

sigma = 1;

F = @(x) 1 - exp(-x.^2/(2*sigma^2)); % your CDF

F_inv = @(u) sqrt(-2*sigma.^2.*log(1-u)); % inverse of CDF

x = rand(10000,1); % uniform random numbers

x_F = F_inv(x); % random numbers according to your CDF

Now to check if this is correct.

[count, bins] = histcounts(x_F);

locs = (bins(1:end-1) + bins(2:end))/2;

simulated_cdf = normalize(cumsum(count), 'range');

actual_cdf = F(locs);

plot(locs, simulated_cdf, 'r', locs, actual_cdf, 'b--')

legend({'Simulated', 'Actual'}, 'FontSize', 14)

Ameer Hamza
on 1 Apr 2020

John D'Errico
on 1 Apr 2020

What are you talking about? Sigma has no need to be determined. The question clearly stated to do this twice, once for sigma^2=1, and once for sigma^2=3. The case where sigma^2=1 just happens to be the same as sigma=1, So, yes, sigma is determined correctly.

As far as the use of uniformly distributed numbers, do some reading. You will learn that this is how the inverse CDF method works. David gave you the place to look.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/514287-how-to-generate-random-variable-x-for-specific-cdf-fx-1-exp-x-2-2-sigma-2-for-sigma-2#comment_819015

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/514287-how-to-generate-random-variable-x-for-specific-cdf-fx-1-exp-x-2-2-sigma-2-for-sigma-2#comment_819015

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/514287-how-to-generate-random-variable-x-for-specific-cdf-fx-1-exp-x-2-2-sigma-2-for-sigma-2#comment_819046

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/514287-how-to-generate-random-variable-x-for-specific-cdf-fx-1-exp-x-2-2-sigma-2-for-sigma-2#comment_819046

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/514287-how-to-generate-random-variable-x-for-specific-cdf-fx-1-exp-x-2-2-sigma-2-for-sigma-2#comment_819118

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/514287-how-to-generate-random-variable-x-for-specific-cdf-fx-1-exp-x-2-2-sigma-2-for-sigma-2#comment_819118

Sign in to comment.