how to generate normally distributed R.V in matlab?

Status
Not open for further replies.
Unfortunately, it is not possible to give you a direct answer, since your requirements are inconsistent. You asked that the variable be (1) normally distributed, and (2) bounded by 0 and 1.
In a rigorous sense, any normally distributed variable is bounded by -inf and +inf. You can see that by inspecting the graphs and equations at https://en.wikipedia.org/wiki/Normal_distribution. Thus it is impossible to generate an exact solution.

However, you could instead say "I want X% of the datapoints to fall within 0 and 1. For example, you could pick 99.7%, which is the 3-sigma value. Since your mu is 0.5, that makes the distance to each bound 0.5, and for a 3 sigma value of 0.5, you just get sigma = 0.5/3 = 0.1667.

This STILL doesn't guarantee that your data points lie within 0 and 1. 3 out of every thousand draws will fall outside. You could either (a) throw them away or (b) make them equal to whatever the closest bound it. Just note that if you do either (a) or (b), your distribution is "technically" not a Gaussian anymore, although it will look very much like one. If you run the code below, you see that the ends of the tails have been chopped off.

If I had to answer the question, this is what I would do:


%define your lower and upper bound
bound = [0 1];
%calculate mu
mu = mean(bound);
%
sigmabound = 3; %how many sigmas to contain

sigma = diff(bound/(2*sigmabound));

%generate the random variable
a = normrnd(mu,sigma, 10000,1);

%remove the points that lie outside the bound
a(a>bound(2)|a<bound(1)) = [];

%plot result
hist(a,100)
 
For normally distributed noise, use the randn() function.

For uniformly distributed noise on the open interval (0,1), use the rand() function.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…