Generate random negative and positive values with specific average
조회 수: 5 (최근 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에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!