Create a random number with max, min and mean (average) in Matlab

I need to generate random numbers with the following properties.

  • Min should be 1
  • Max should be 9
  • Average (average) - 6.00 (or something else)
  • Random number should only be integer (positive)

I tried several syntaxes but nothing works like

r=1+8.*rand(100,1); 

This gives me a random number between 1-9, but it is not an integer (for example, 5.607 or 4.391), and every time I calculate the average, it changes.

+4
source share
6 answers

Here is a loop algorithm to achieve the required average xmean (with the required xeps accuracy) by regenerating a random number from one half of the vector to the other according to the average value in the current iteration. With my trials, he reached the average quite quickly.

 n = 100; xmean = 6; xmin = 1; xmax = 9; xeps = 0.01; x = randi([xmin xmax],n,1); while abs(xmean - mean(x)) >= xeps if xmean > mean(x) x(find(x < xmean,1)) = randi([xmean xmax]); elseif xmean < mean(x) x(find(x > xmean,1)) = randi([xmin xmean]); end end 

x is the result you need.

+2
source

You can define a function that satisfies your requirements based on the Matlab randi function. But be careful, it's easy to define the functions of random number generators that don't produce random numbers.

Another approach might come up - create a probability distribution to meet your requirements. In this case, you need a vector of 9 floating point numbers that add up to 1 and which, individually, express the probability of the i-th integer occurring. For example, the distribution can be described by the following vector:

 [0.1 0.1 0.1 0.1 0.2 0.1 0.1 0.1 0.1] 

They divide the interval [0,1] into 9 parts. Then take your favorite rng, which generates floating point numbers in the range [0,1) and generates a number, suppose it is 0.45. Read the interval from 0 to 1 and you will find that it is in the 5th interval, so return the integer 5.

Obviously, I'm too lazy to give you a vector that gives 6 as the middle part of the distribution, but it shouldn't be too hard for you to figure out.

+4
source

You can use randi to get random integers

+2
source

You can use floor to truncate your random numbers with integer values ​​only:

 r = 1 + floor(9 * rand(100,1)); 

Getting a specific value is a bit more complicated; it depends on which distribution you use.

+1
source

Before you can create your random number generator, you need to specify the distribution from which it should extract. You only partially did it: i.e. You indicated that it extracts from integers in [1.9] and that it has the average value that you want to specify. This still leaves an infinity of distributions to choose from. What other properties do you want in your distribution?

Change the following comment: The average of any final sample from the probability distribution β€” the so-called sample mean β€” will only approximate the average of the distribution. There is no such thing.

As was said, the simplest (in the sense of maximum entropy) distribution over integers in the region [1,9] is the exponential distribution: i.e.

 p = @(n,x)(exp(-x*n)./sum(exp(-x*(1:9)))); 

The parameter x determines the average value of the distribution. Corresponding cumulative distribution

 c = cumsum(p(1:9,x)); 

To extract p from the distribution, you can draw a random number from [0,1] and find which sub-interval in c it falls into: ie,

 samp = arrayfun(@(y)find(y<c,1),rand(n,m)); 

returns an array of [n,m] integers taken from p .

+1
source

If the distribution is not important, and all that interests you is the average, then there is a particularly simple function that does this:

 function x=myrand x=6; end 
+1
source

Source: https://habr.com/ru/post/1402669/


All Articles