Generate random negative and positive values with specific average
4 views (last 30 days)
Show older comments
Hello,
Please help me with the following:
I would like to create an array (e.g. 1 x 10) with random numbers both negative and positive where the overall average value of the 10 values is predefined, e.g. 0.25;
At first, the number of negative and positive values of the array is random.
Also, is it possible to predefine the number of negative and positive values, for example 3 negative and 7 positive?
Thank you.
Best,
Pavlos
0 Comments
Accepted Answer
KSSV
on 19 Aug 2017
a + (b-a)*rand() will give uniform random values in the interval [a,b] with mean (b+a)/2. You select a and b such that mean is 0.25.You can select negative numbers also.
0 Comments
More Answers (1)
John D'Errico
on 19 Aug 2017
Edited: John D'Errico
on 19 Aug 2017
This the 1 millionth time I've seen the question. In fact, this question has at least several problems that make it not well posed.
First of all, you cannot say that you want to generate random numbers, without specifying the distribution of those numbers.
Next, however you generate the random sample, the mean will in general not be EXACTLY the mean of the distribution. So the suggestion by KSSV will probably be useless if you want an exact mean.
Next, you can do tricks with the sum to yield an exact mean, but that screws around with the distribution. So if you wanted a uniform random sample, but then you adjust the mean afterwards by scaling or an additive shift, that screws up the distribution. It is no longer uniform.
There are tools like randfixedsum (download from the FEX) to solve the general problem of sampling with an exactly fixed sum, it won't allow you to constrain the number of positives and negatives.
There is no simple way to ensure a fixed number of negatives and positives in such a sample.
See Also
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!