CS 11 -- Lab 5

We're going to use arrays for a bit of statistical testing of random numbers.


Overview

When we ask Math.random() to generate a random number, is it really random? Nope. Computers always follow a deterministic sequence of steps, so they cannot produce something that is truly random. (A question for discussion with friends when you've stayed up too late: Is there such a thing as true randomness?)

Computers, instead, use pseudo-random number generators, which are algorithms that produce a sequence of numbers that are so jumbled that you cannot predict the values. However, is the random number generation provided in Java good? Let's create a bunch of random numbers and take a step or two towards finding out.


Your assignment

Our goal will be to generate a large group of random numbers, and then to determine what the mean and the distribution of those numbers are. Follow these steps:

  1. Use emacs to create a file called RandomTest.java, which should contain the program named (unsurprisingly) RandomTest.

  2. Write a method named createRandomArray that creates an array of double values, where the size of this array should be specified by the caller. The method should assign each element in the array with a random number drawn from the Math.random() method. It should return a pointer to the newly created array.

  3. Write a method named showArray that prints some number of entries from an array. Specifically, the array and the number of leading elements to be printed should be provided by the caller.

  4. Optional step: To ensure that the first two methods that you've written work, write a main method that will test them. Specifically, main should call on createRandomArray to create an array of 1,000 random double values. It should then call on showArray to print the first 15 entries of the newly created array. Compile and run this code, ensuring that it works before you move to the next step.

  5. Now write a method named computeMean that takes computes with average of the elements in an array of double. The array should be provided by the caller, and the average should be returned to the caller.

  6. Optional step: Add, to your main method, a call to computeMean so that it generates the average of the 1,000 random values. Have main print that average. Compile and run your code before moving onto the next step.

  7. Write a method named computeDistribution that accepts an array of double and determines how many values fall within certain intervals of the range of random values produced by Math.random. Specifically, you need an array of int called counts, whose size is 10. counts[0] should hold the number of values in the array of double that fall between [0.0, 0.1). Similarly, counts[1] should be assigned the number of values in the array of double that fall between [0.1, 0.2). The last entry of counts corresponds to the interval [0.9, 1.0).

    This method should create and return counts, where the values assigned into that array are determined by the array of double passed by the caller.

    For example, if the first 5 entries of the array of double are...

    0.99999 0.34322 0.7222818 0.11918 0.355543

    ...then the counts array would look like...

    index: 0 1 2 3 4 5 6 7 8 9
    value: 0 1 0 2 0 0 0 1 0 1

    ...because we saw 0.11918 in the range (.1, .2], and both 0.34322 and 0.355543 in the range (.3, .4], and so forth.

  8. Add to main a call to computeDistribution, passing it the array of random values. Also add a loop that will print the contents of the array of int returned by computeDistribution.

  9. Compile and run this code. If the pseudorandom number generator is a good one, then each entry of the array of int should be near 100, and overall should be about evenly balanced around 100 (some high, some low). Do the numbers produced by Math.random appear to be evenly distributed through their range?


Submit your work

Submit, as lab-5, your RandomTest.java file using the cs11-submit command.


This assignment is due at 11:59 pm, Thursday, 28-October-2004

Scott F. Kaplan
Last modified: Wed Oct 20 12:47:32 EDT 2004