MATLAB Answers

Generating random numbers from normal distribution

361 views (last 30 days)
Ruby
Ruby on 25 Jun 2013
Answered: Mostafa Nakhaei on 18 Nov 2019
Hello,
I generated random numbers from normal distribution for a parameter that has typical values within the range 0.0 to 0.4. The generated random numbers have both negative and positive values. How do I generate only positive values to fit the range of my parameter?
I have another concern:
I understand the random numbers generated from normal distribution in matlab actually come from standard normal distribution. Is there a way to generate from the normal distribution?
Thanks in advance
Best Regards

  1 Comment

Sign in to comment.

Accepted Answer

Iain
Iain on 25 Jun 2013
If you take a random number from a gaussian (aka normal) curve, you can calculate the probability that number would come up.
random = randn();
prob = icdf('Normal', random_value, 0, 1);
You can then, if you know the cdf, calculate the value that would give you that probabiltiy.
example: random_value_my_distribution = cdf('binomial',prob, trials, prob)

  0 Comments

Sign in to comment.

More Answers (4)

Leah
Leah on 25 Jun 2013
Yes you can do this you just need the right transformation. You cannot generate a bounded normal distribution. It needs to be defined with the mean and standard deviation. So your mean would be 0.2, you just shift the distribution by this amount. If you want a bounded distribution try a triangular or uniform.
standard deviation =0.1 mean = 0.2
r = 0.2 + 0.1.*randn(100,1);

  0 Comments

Sign in to comment.


Shashank Prasanna
Shashank Prasanna on 25 Jun 2013
Edited: Shashank Prasanna on 25 Jun 2013
I'd like to clarify could of things.
"random numbers generated from normal distribution in matlab actually come from standard normal distribution"
This is true only if you use randn If you want to use uniform random numbers then you have to use rand
Non-standard normal random number can be generated as follows:
mean + sigma*randn();
Uniform random random numbers on a separate interval (not 0-1) between a and b can be generated as follows:
r = a + (b-a).*rand();
This way you can specify your own range and keep it positive if you like.

  0 Comments

Sign in to comment.


Ruby
Ruby on 27 Jun 2013
Hi, Thank you all for the response. I found the answers from Lain and Shashank to be very applicable and I have been able to generate the random variables within the range I wanted. Once again, thanks for all help.

  0 Comments

Sign in to comment.


Mostafa Nakhaei
Mostafa Nakhaei on 18 Nov 2019
The best answer is to simply not consider the side that produce negative results using if statement.
So, generate the whole numbers and then do not consider the left side!
Thanks

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!