Hi everyone, I'd like to know how to generate a set of random numbers with normal distribution (randn) belonging to a range defined by me.
To make it easier:
I want to generate 10 random numbers between [-0.5 0.5] using randn.
Thanks for your attention

 채택된 답변

Jeff Miller
Jeff Miller 2020년 5월 25일

0 개 추천

Good. Here it is as an 'official' answer:
pd = makedist('Normal');
t = truncate(pd,-0.5,0.5);
r = random(t,10,1);

추가 답변 (1개)

KSSV
KSSV 2020년 5월 23일

1 개 추천

a = -0.5 ;
b = 0.5 ;
N = 10 ;
r = (b-a).*randn(N,1) + a;

댓글 수: 7

Daniele1
Daniele1 2020년 5월 23일
thx
Jeff Miller
Jeff Miller 2020년 5월 24일
ummm...that isn't right for normal random numbers, only uniform(0,1). The code given will generate numbers from about -4 to 3 with a mean of -0.5, which is not what the OP was asking for.
Daniele1
Daniele1 2020년 5월 24일
Could please help me ? I noticed it was wrong when i tried to run it on my project
Jeff Miller
Jeff Miller 2020년 5월 25일
I am not sure exactly what you want to do. One possibility is to just keep generating random numbers with randn until you find 10 in the range you want. Would that work for you?
Daniele1
Daniele1 2020년 5월 25일
It would work. Are you sure that there are no possibilities to generate numbers between a specified range?
If that would work, then I guess this is what you are after:
pd = makedist('Normal');
t = truncate(pd,-0.5,0.5);
r = random(t,10,1);
Daniele1
Daniele1 2020년 5월 25일
it works perfectly! thanks

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

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2020년 5월 23일

답변:

2020년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by