Clear Filters
Clear Filters

randi generate a normally distributed integer matrix

14 views (last 30 days)
I encountered a problem while studying the courses officially provided by matlab. The question requires using the randi function to generate a normally distributed rather than uniformly distributed integer matrix. After consulting the help file of the randi function, I did not find this function.
  4 Comments
Dyuman Joshi
Dyuman Joshi on 25 Oct 2023
You might have misread it as integers rather than numbers.
There is a function that generates normally distributed random numbers i.e. randn.
However, this is only a guess. For a definite feedback, please provide the information @Steven has asked in the comment above.
John D'Errico
John D'Errico on 26 Oct 2023
Edited: John D'Errico on 26 Oct 2023
Can you use randi to generate a normally distributed matrix? Of course not. You may be mistaken. Or the person writing the question may have not understood what they are asking. After all, people are only human. Even those who write questions as part of courses or textbooks, even teachers.
You could use randi as part of something to generate a sample that will APPROXIMATE a normal distribution, with some effort. I can think of several ways to do that.
If you can show/quote the actual quetion, we might be able to help you. But doing what you seem to think you want to do is not possible.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 26 Oct 2023
The following illustrates that it is possible to create an array of integers whos population statistics approximate normal distribution. The larger the population generated, the more the array (here called discrete) would approximate normal distribution.
This does not show you anything about how to create such a distribution starting with randi() -- just that there are meaningful senses in which it could exist.
rng('shuffle')
N = 1000;
continuous = randn(N,1);
scalefactor = (2^53 - 1);
bins = -1:.1:1;
discrete = round(continuous * scalefactor);
descaled = discrete ./ scalefactor;
sdc = std(continuous)
sdc = 0.9900
sdd = std(descaled)
sdd = 0.9900
sdi = std(discrete)
sdi = 8.9170e+15
r = sdi / sdc
r = 9.0072e+15
scalefactor / r
ans = 1.0000
histogram(continuous, bins)
histogram(descaled, bins)
histogram(continuous - descaled)
histogram(discrete, bins*scalefactor)
[H,P,CI,STATS] = ttest2(continuous, descaled)
H = 0
P = 1
CI = 2×1
-0.0868 0.0868
STATS = struct with fields:
tstat: 0 df: 1998 sd: 0.9900
  2 Comments
Bruno Luong
Bruno Luong on 26 Oct 2023
Edited: Bruno Luong on 26 Oct 2023
One could use the well known central limit theorem, no need to mess with number bit coding and poor results.
But that is not the point. It is still not a normal distribution strictly speaking
n=1000000;
ns=100; % larger -> better approximation
r=sum(randi([-1000 1000],ns,n))/(sqrt(sum((-1000:1000).^2)*ns/(2001)));
histogram(r,'Normalization','pdf');
x=linspace(-3,3);
hold on
plot(x,1/sqrt(2*pi)*exp(-x.^2/2),'g','linewidth',2)
Walter Roberson
Walter Roberson on 26 Oct 2023
Any finite sampling of normally distribute samples will not be normally distributed "strictly speaking".

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!