Generate random numbers with specific mean

조회 수: 7 (최근 30일)
Amr Ayman
Amr Ayman 2023년 5월 8일
편집: Matt J 2023년 5월 9일
I want to generate matrix 4*4 of random integer values from 0 to 255, i used randi([0 255],4) but i want to generate it with specific mean for example 200 so i used:
z = randi([0 255],4)
z = z + (200 - mean2(z))
that did fix mean value but it could change min and max value of integers, How can i fix it ??!
  댓글 수: 2
the cyclist
the cyclist 2023년 5월 8일
편집: the cyclist 2023년 5월 8일
There are an infinite number of ways to generate random integers that have the following properties:
  • max = 255
  • min = 0
  • mean = 200
For example,
N = 1000000;
p = 200/255;
x = 255 * (rand(N,1) < p);
mean(x) % Will be equal to 200, within sampling error
ans = 200.2543
figure
histogram(x)
This distribution obeys the rules you have told us. And, as I said, there are infinite others. You need more specificity on the distribution.
Amr Ayman
Amr Ayman 2023년 5월 8일
you're right, I should have been more specific about what i wanted but I couldn't explain what I needed more than that

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2023년 5월 8일
편집: Matt J 2023년 5월 8일
You can use this FEX download
m=4;n=4; targetMean=200;
z = randfixedsum(m*n,1,targetMean*m*n,0,255);
z=reshape(z,m,n);
mean(z(:))
ans =
200.0000
  댓글 수: 7
Amr Ayman
Amr Ayman 2023년 5월 9일
Thanks, I will look at it
Matt J
Matt J 2023년 5월 9일
편집: Matt J 2023년 5월 9일
There is a difference between
  • flip 10 coins, and on average get 5 tails
  • flip 10 coins, and always get 5 tails
There can be a difference, but note that the second always implies the first.
And yeah it generates real values not integer so i added floor after reshaping "which will make value of mean slightly change around 200 not to be always 200 :)) "
In fact, if you apply round() instead of floor(), I would expect the statistical average to remain at 200, even if the nonstatistical average mean(z(:)) does not remain perfectly at 200.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by