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:
- The number of sides to a die: If a die has 6 sides, then the possible
roll values are 1, 2, 3, 4, 5, and 6.
- The number of times a die has been rolled
- The frequency of various die rolls: The die should keep track of how
many times each value has been the result of a roll
You will complete the Dice class. Check out Labs/06_lab and open up Dice.java.
- Complete the constructor so that initializes the private instance
variables and any other necessary variables.
- Complete the roll method so that it returns a random value
between 1 and the number of sides on a Die (inclusive).
- 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.
- 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.
- 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.
- 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 (
) statistic for an experiment with
possible outcomes, performed
times, in which
are the number of experiments which resulted in
each possible outcome, with probabilities of each outcome
is:
will be larger to the extent that the observed results
diverge from those expected by chance.
Q is the probability that
that
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
is an approximation which is valid only for large values
of
, and is therefore only meaningful when calculated from a large
number of independent experiments.
.
- 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:
- 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