Generate random numbers with specific mean

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 comentarios

the cyclist
the cyclist el 8 de Mayo de 2023
Editada: the cyclist el 8 de Mayo de 2023
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 el 8 de Mayo de 2023
you're right, I should have been more specific about what i wanted but I couldn't explain what I needed more than that

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 8 de Mayo de 2023
Editada: Matt J el 8 de Mayo de 2023
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 comentarios

Amr Ayman
Amr Ayman el 8 de Mayo de 2023
this is what i needed, Thanks alot
the cyclist
the cyclist el 8 de Mayo de 2023
I would encourage you to think carefully about whether this function truly does what you need.
Specifically, this function will return values that always sum to 200. This is different from saying you want to draw values from a distribution that will give a mean value of 200.
Draw an analogy with coin-flipping: There is a difference between
  • flip 10 coins, and on average get 5 tails
  • flip 10 coins, and always get 5 tails
The randfixedsum function is more like doing the latter. Maybe that's what you need, but maybe not.
the cyclist
the cyclist el 8 de Mayo de 2023
Also, randomfixedsum generates real values, not integer values.
Amr Ayman
Amr Ayman el 8 de Mayo de 2023
Thanks for your comment but yes it was what i wanted
As it will generate different matrix each time but with the specific mean i gave it, Yeh it will always get 200 not 200 on avergae but it's okay for me.
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 :)) "
the cyclist
the cyclist el 9 de Mayo de 2023
OK.
FYI, there is something known as the beta-binomial distribution that I believe would also have the properties you described (but with integer values inherently). I don't think MATLAB has an implementation of it, but it looks like this File Exchange submission does it.
Amr Ayman
Amr Ayman el 9 de Mayo de 2023
Thanks, I will look at it
Matt J
Matt J el 9 de Mayo de 2023
Editada: Matt J el 9 de Mayo de 2023
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Mayo de 2023

Editada:

el 9 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by