13.5 Uniform (Continuous Version)
Suppose instead of flipping a coin with 2 outcomes, you want to simulate test scores of students, where any score between 0 and 100 is possible.
As this random variable \(X\) is continuous, we define its probability with its density function:
\[ f_{X}(x) = \left\{ \begin{array}{ll} \frac{1}{100 - 0} & \mbox{ for } 0 \le x \le 100 \\ 0 & x < 0 \mbox{ or } x > 100 \end{array} \right. \]
In the fraction I include the two endpoints (0 and 100) to illustrate how you would adjust for other bounds. These bounds are defined as the parameters of the uniform distribution.
As an aside, this definition makes sense, as if you are asked what is the chance that someone’s exam score is between 0 and 25, you would say 1/4, as the length of the interval between 0 and 25 is one-quarter of the length between 0 and 100.
To simulate one exam score we would use the following code:
runif(n = 1, min = 0, max = 100)
You could save the results in another variable for use.
<- runif(n = 100, min = 0, max = 100) dat
Then you can check on the mean (average) of the sample. What result to you expect to find?
mean(dat)
And provide a histogram of the data to see the frequency of occurence. (You can play the results to see what happens is you lower the sample size or raise it to 5000.)
hist(dat)