Normally distributed Random numbers generator issue

I've used the randn function (setting rng(1)) and plotted the histogram:
% ----------------
rng(1)
figure
hist(randn(500,1),50)
% ------------------------
the result I've got in Matlab 2020b was rather far from a normal distribution.
Increasing the amount of generated numbers helps a bit - but not enough.
Any ideas on how to get a more adequate, normally distributed, bumbers ?

답변 (4개)

Veronica Taurino
Veronica Taurino 2021년 3월 24일
편집: Veronica Taurino 2021년 3월 24일
The following (but also yours, from my pov) seems quite normal to me, why is it not ''enough'' normal for you?
figure
number = 1000000
hist(randn(number,1),50)
Jan
Jan 2021년 3월 24일
편집: Jan 2021년 3월 24일

1 개 추천

This is a perfect normal distribution of 500 values. With such a small set of data, a lot of noise is expected. Otherwise it would not be a random normal distribution.
So the only problem is, that you expect something else, which does not match the defition of normally distributed randomness.
So what do you need? Do you want a perfect normal distribution without randomness, e.g. by just shuffling the values?
Stanislav Lifshitz
Stanislav Lifshitz 2021년 3월 24일

0 개 추천

The random sequence I've generated was for 500 values, which isn't a small set.
I understand that randomizing a sequence of 1e6 values will generate a normally distributed sequence (There is a theorem in math to support this claim)
However - the analysis I need to do based on these values is quite compuattationally demanding and calculating this for 1e6 values isn't an option.
So - my question is can I get a noramlly disitributed set of 500 values from 'randn' or should I be looking for alternate ways to get a normally distruibuted set ?
Thanks a lot for your thoughts on the matter.

댓글 수: 5

rng(1);
data = randn(500,1);
mean(data)
ans = 0.0086
std(data)
ans = 1.0138
What would be an acceptable mean and std() for your purposes?
hist(data,50)
histogram(data,50)
Notice that your binning has made a noticeable difference.
Hi Walter,
It's not as much about mean and std values as it is about the distribution ( if I take 250 values of (-1) and 250 values of (+1) - I'll get a mean value of 0 and std of 1).. if the distribution isn't Gaussian (Normal) than the values of mean and std do not represent the probability function.
However, the difference in the binning algorithm that you've pointed out actually makes the data look better.
Thanks a lot.
rng(1);
data = randn(500,1);
[h, p, ksstat, cv] = kstest(data)
h = logical
0
p = 0.7039
ksstat = 0.0312
cv = 0.0604
kstest() cannot distinguish the distribution from
Jan
Jan 2021년 3월 24일
@Stanislav Lifshitz: Again: randn(500,1) is a perfect random distribution already. If you want the shape of the values to be a a perfect bell wave, the values are not random anymore.
Your question is similar to asking for throwing a set of 6 dice and having the need to get each value 1 time. Of course this works, but then it is not random anymore.
I totally agree with Jan. You have been confused by the shape of the data, perhaps? But for definition, it is a normal distribution (as Walter has pointed out, mean 0 and std 1). If you just want to change the shape, it is not randon anymore.

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

Jeff Miller
Jeff Miller 2021년 3월 24일
As the other posters have said, you aren't necessarily going to get an exactly normal-looking distribution if you generate the numbers randomly. But you can nonrandomly generate an exactly normal-looking set of numbers like this:
nPoints = 500;
cdfvals = (0.5:1:nPoints) / nPoints;
evenRand = norminv(cdfvals);
figure;
hist(evenRand,50);

제품

릴리스

R2020b

질문:

2021년 3월 24일

댓글:

2021년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by