# Generating random numbers from normal distribution

조회 수: 32 (최근 30일)
Ruby 2013년 6월 25일
댓글: Pavan Karuturi 2023년 4월 18일
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?
Best Regards
##### 댓글 수: 2없음 표시없음 숨기기
Walter Roberson 2013년 6월 25일
Take abs() of your generated number, perhaps?
Pavan Karuturi 2023년 4월 18일
How to Generate Gaussian Random Variable in MATLAB? Also plot its CDF and PDF.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Iain 2013년 6월 25일
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)

댓글을 달려면 로그인하십시오.

### 추가 답변 (5개)

Leah 2013년 6월 25일
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이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Shashank Prasanna 2013년 6월 25일
편집: Shashank Prasanna 2013년 6월 25일
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이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Ruby 2013년 6월 27일
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이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Mostafa Nakhaei 2019년 11월 18일
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
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Alireza Ahani 2021년 4월 24일
편집: Alireza Ahani 2021년 4월 24일
In that case, it is not normal distribtion, we would have an arbitrary PDF for distribution:

댓글을 달려면 로그인하십시오.

Alireza Ahani 2021년 4월 24일
If you want to nessecarily have a "normally (gaussian/bell-shaped) pdf" for generation of the random number, you can use this code:
YLIM = [0.0 0.4];
N=100; % number of random vars
n=3.2; % parameter for adjusting sigma
mu=0.2; % mean
sigma = (YLIM(2)-mu)/n;
x=-5:0.001:5;
y = normpdf(x,mu,sigma);
figure; plot(x,y); ylabel('%'); title('pdf'); xlim(YLIM);
text(YLIM(1),0.9*max(ylim),['probability to be outside of desired limit=' num2str(100*(1-erf(n/sqrt(2)))) '%'])
RndN = mu + randn(N,1).*sigma;
figure; plot(RndN);
you can adjust "n" to have a control over probability of overpassing the limits [0.0 0.4], it is based on this reference:

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Uniform Distribution (Continuous)에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by