Random number generate in an interval

Hi community,
I would like to generate random number in the follow distribuition: Normal, Gamma and Weibull.
Amount of data required: 5000 numbers at interval [a,b]
At the example, it was used the follow code:
A = linspace(0,1,5000)
sr = size(A)
b = normrnd(27.5,15.73,sr)
plot(b)
Its works, however, the valid interval to generate was [1,54]
Can us help me, please?
Yours faithfully

7 Comments

William Rose
William Rose on 14 Sep 2023
Edited: William Rose on 14 Sep 2023
[edit: fix spelling errors]
It is not possible to generate random numbers that follow the normal or Weibull or gamma distributions, and limit them to a finite range, because all of those distributions have non-zero probability out to infinity. The latter two go from 0 to +Inf, and the normal from -Inf to +Inf.
If you specify a mean and s.d. for normal, or a shape and a scale for the Weibull and gamma, you can generate random numbers, and then you can clip any values outside your interval of interest. Technically, once you have done this, the remaining numbers do not have the originally specified distribution. In practice, the clipping won't make a difference, if the probability of extreme values was extremely small anyway, compared to the sample size.
I hope this helps.
(27.5 - 1) / 15.73
ans = 1.6847
(54 - 27.5) / 15.73
ans = 1.6847
Less than 2 standard distributions -- the excluded area is non-trivial, and will make a significant difference in normality.
A = linspace(0,1,5000)
A = 1×5000
0 0.0002 0.0004 0.0006 0.0008 0.0010 0.0012 0.0014 0.0016 0.0018 0.0020 0.0022 0.0024 0.0026 0.0028 0.0030 0.0032 0.0034 0.0036 0.0038 0.0040 0.0042 0.0044 0.0046 0.0048 0.0050 0.0052 0.0054 0.0056 0.0058
sr = size(A)
sr = 1×2
1 5000
b = normrnd(27.5,15.73,sr);
[min(b), max(b)]
ans = 1×2
-45.4716 84.9146
pd = fitdist(b.','Normal')
pd =
NormalDistribution Normal distribution mu = 27.3657 [26.9338, 27.7975] sigma = 15.5758 [15.2764, 15.8872]
btrunc = max(1, min(b, 54));
[min(btrunc), max(btrunc)]
ans = 1×2
1 54
pd2 = fitdist(btrunc .', 'Normal')
pd2 =
NormalDistribution Normal distribution mu = 27.383 [26.9857, 27.7802] sigma = 14.3283 [14.0529, 14.6148]
The standard distribution of the truncated version is notably different.
@Walter Roberson is exactly right that if and , then clipping to [1,54] leaves a distinctly non-normal distribution. I was not sure from your orignal post if the code
b = normrnd(27.5,15.73,sr)
was just a separate example, or if it represented the mean and SD which you actually want. You will need to calculate a bit to translate a desired mean and SD to shape and scale parameters for gamma (easy) or Weibull (less easy).
Careful
b = normrnd(27.5,15.73,sr);
btrunc = max(1, min(b, 54));
This clipping does NOT generate truncated normal distribution which is conditional probability (rejection) and NOT clipping probability
To illustrate @Bruno Luong's point
rng(100)
pd = truncate(makedist('Normal',27.5,15.73),1,54);
x = 1:.01:54;
figure
plot(x,pdf(pd,x))
hold on
b = normrnd(27.5,15.73,[1 500000]);
btrunc = max(1, min(b, 54));
histogram(btrunc,'Normalization','pdf')
figure
plot(x,pdf(pd,x),'LineWidth',5)
hold on
breject = b(b>=1 & b<=54);
histogram(breject,'Normalization','pdf')
Right, different interpretations of what "clipping" means in the context.
format long g
b = normrnd(27.5,15.73,[1 500000]);
out_of_range = (b < 1) | (b > 54);
mean(out_of_range) * 100
ans =
9.195
so over 9% of the samples are outside of the target range
It works! Thank you very much

Sign in to comment.

Answers (2)

the cyclist
the cyclist on 14 Sep 2023
Edited: the cyclist on 14 Sep 2023
A normal distribution, by definition, has support from negative infinity to positive infinity. You cannot have both a normal distribution and a finite range.
You can use the truncate function to create a truncated normal distribution object, and draw values from that. (See the second example on that page.) Is that what you want?
This doesn't need stat toolbox.
sigma = 15.73;
mu = 27.5;
interval = [1 54];
n = [1, 5000000];
X = TruncatedGaussian(-sigma,interval-mu,n) + mu;
histogram(X)

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!