Generate random numbers with conditions (min, max, mean, and specific values)
Show older comments
Hi! I'm very new at Matlab and I would like some tips to solve the following issue ...
I would like to generate a sequence of 40 random numbers (floats) (min=8, max=20, mean=10). However, 4 of these numbers must be equal to 16. How do you solve this question?
6 Comments
John D'Errico
on 21 Nov 2022
It cannot be done, UNLESS you specify the distribution of the numbers you have not chosen already.
So pick 4 numbers equal to 16. Don't worry about the order, since you can always randomize the set.
What is left, now you have a set of random numbers, but with no distribution specified. Random means nothing, if you do not specify the distribution. Yes, you could compute the mean of the numbers that remain, but that is irrelevant.
carolina travassos
on 21 Nov 2022
the cyclist
on 21 Nov 2022
A normal distribution, by definition, does not have a minimum or maximum possible value. So, the things you say that you want are inconsistent.
Perhaps you can describe why you need these numbers, and we can help figure out your confusion.
carolina travassos
on 21 Nov 2022
the cyclist
on 21 Nov 2022
I will also try to be clearer. You cannot have both of these things:
- numbers between 8 and 20
- numbers from a normal distribution
These are mathematically contradictory facts. It is like saying you want a number that is both bigger than 3, and smaller than 2. It just cannot be.
You need to figure out how to reconcile these facts. Often, people don't really understand what a normal distribution is, and they might be OK with some other random distribution. Or sometimes they are OK with a truncated normal. But, we cannot figure that out for you.
Walter Roberson
on 22 Nov 2022
https://www.mathworks.com/matlabcentral/answers/886094-normally-distributed-random-numbers-with-fixed-sum#answer_754514 might help with generating the 4 values that have a fixed sum. Then you generate another 36 values. Then you randperm() the vector of 40 values.
Answers (2)
Dev
on 28 Nov 2022
You might be aware that at least 4 numbers are going to be 16, so you have to generate 36 random numbers with mean 28/3. If type of distribution doesn’t matter, You can use the following code to generate such distribution.
n = 36;
xmean = 28/3;
xmin = 8;
xmax = 20;
xeps = 0.01;
x = randi([xmin xmax],n,1);
while abs(xmean - mean(x)) >= xeps
if xmean > mean(x)
x(find(x < xmean,1)) = randi([ceil(xmean) xmax]);
elseif xmean < mean(x)
x(find(x > xmean,1)) = randi([xmin floor(xmean)]);
end
end
Bruno Luong
on 28 Nov 2022
Use this fex
https://fr.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
r = randperm([16+zeros(4,1); randfixedsum(36,1,40*10-16*4,8,20)]);
p=randperm(40);
r=r(p)
Most of numbers are around 10 since the MEAN pull the numbers arouns that value, so the chance to get big number (20) is tiny.
Categories
Find more on Audio and Video Data 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!