Generate random negative and positive values with specific average
8 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
KSSV
2017 年 8 月 19 日
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 件のコメント
その他の回答 (1 件)
John D'Errico
2017 年 8 月 19 日
編集済み: John D'Errico
2017 年 8 月 19 日
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.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!