Lab 6: In-Lab Exercises

Taken from Fourmilab's Retropsychokinesis page on Probability and Statistics.

Dice

You have been given a Dice class.The Dice class simulates a die and keeps associated statistics.

A Dice object (Die) needs to keep track of:

You will complete the Dice class. Check out Labs/06_lab and open up Dice.java.
  1. Complete the constructor so that initializes the private instance variables and any other necessary variables.
  2. Complete the roll method so that it returns a random value between 1 and the number of sides on a Die (inclusive).
  3. Complete the getFrequency method that returns the percentage of rolls a particular value has come up.

Is it Random?

How do you determine if a die is random? Two basic methods involve using the arithmetic mean and the chi square statistic.

Arithmetic Mean

Calculating the average or arithmetic mean of the numbers generated gives some idea of whether the numbers are random or not. For example, for a six-sided die, one would expect the mean to be

(1 + 2 + 3 + 4 + 5 + 6)/6 = 3.5

One measure of the randomness of a sequence of die rolls is how close the actual mean of the die rolls is to the expected mean.

  1. Implement the mean method in the Dice class. You should also add code to the main method to check whether the query the mean and determine how close the observed mean is to the expected mean.

  2. It is possible write mean without iterating through the roll counts. Change the roll method, so that the mean can be determined without any loops. You may add any necessary instance variables.

  3. How is the arithmetic mean a flawed measure of randomness? Give an example where the rolls are not random (i.e. equally likely) but the mean is correct. Complete your answer to this question in your README.

Chi-Square

The chi-square ($\chi^2$) statistic for an experiment with $k$ possible outcomes, performed $n$ times, in which $Y_1, Y_2,... Y_k$ are the number of experiments which resulted in each possible outcome, with probabilities of each outcome $p_1, p_2,... p_k$ is:

\begin{displaymath}
\chi^2 = \sum_{1 \leq i \leq k} \frac{(Y_i - n p_i)^2}{n p_i}
\end{displaymath}

$\chi^2$ will be larger to the extent that the observed results diverge from those expected by chance.

Q is the probability that that $\chi^2$ will be larger to the extent that the observed results diverge from those expected by chance. Q is approximated using the chiSquaredProbability method. Note that the probability calculated from the $\chi^2$ is an approximation which is valid only for large values of $n$, and is therefore only meaningful when calculated from a large number of independent experiments. .

  1. Implement the chiSquare method. Write calls to chiSquare in main that test the report the chiSquare value and chiSquareProbability after a one million trials. Do the values change on successive runs? Why?

Better Random

Instead of using the java.util.Random, one could use a true random number generator like the one provided by random.org. The source of randomness used by random.org is atmospheric noise. We have provided you with the RandomOrg class written by Olaf Blömer. You construct a RandomOrg object and ask nextInt random values in the same way as java.util.Random. However, RandomOrg queries the random.org website for its random bits. The computer at random.org sits around listening to atmospheric noise and returns random data based on the noise's values.

Answer the following question in your README:

  1. What happens to your results if you use the RandomOrg class instead of java.util.Random? Are the mean and chi square values changed?

Submitting

Submit Dice.java and README with assignment name lab06.
Jeffrey R.N. Forbes
Last modified: Mon Oct 4 14:00:20 EDT 2004