In: Computer Science
Use a random number generator to produce 1000 uniformly distributed numbers with a mean of 10, a minimum of 2, and a maximum of 18. Obtain the mean and the histogram of these numbers, and discuss whether they appear uniformly distributed with the desired mean.
We need to generate the 1000 uniformly distributed random numbers with minimum of 2 and maximum of 18. Then we need to check it by plotting histogram.
In MATLAB, the function rand(m, n) generates uniformly distributed random numbers between 0 and 1. Now, since we need an array of 1000 numbers, in our case m = 1 and n = 1000. Also, we need minimum value of 2. That can be achieved by adding 2 to the array created. Then we need maximum value of 18. That can be achieved by multiplying the difference i.e. 16 to the array created. Therefore, the formula for getting the a uniformly distributed number in interval [a, b] is:
(b – z)y + a
Where
y is the random number generated
The MATLAB code is shown below:
Input:
a = 2;
b = 18;
y = (b-a)*rand(1, 1000) + a;
mean(y)
hist(y, 16)
We have also checked the array generated by calculating mean and the histogram
Output:
ans =
10.1841
We see that the mean is 10.1814, which is very close to the mean of 10 in question.
The histogram is shown below:
Histogram is shows that all the bins are nearly of equal length with slight variations. Hence, this can be approximated as a uniform distribution.
We see that the mean is 10.1814, which is very close to the mean of 10 in question.