ALG0183 Algorithms and Data Structures Laboratory 3 A random number generator can be tested by visually examining scatter diagrams of {(xi, xi+1)} or {xi, xi+2)}, etc. If the sequence of numbers from the random number generator is random, you would expect to see points randomly scattered. Any structure indicates non-randomness and possibly degeneracy (not full period) in the random number generator. Part A Execute the applet on the ALG0183 website by Peter Alfeld and answer questions 1-7. The formula for a linear congruential generator is normally written: xn = (a * xn-1) mod M xn = (P1 * xn-1 + P2) mod N In the diagram below, N is M, P1 is a, and P2 is a constant term added to the product before the mod operation. Normally P2 is zero. The “<”, “>” buttons single step through values and the “<<”, “>>” buttons step through by larger amounts. When interacting with the control window it is important to press the enter key if you make a direct change to a number. You can vary the dot size and the number of dots to be displayed. You will not need to look at more than one cycle at once in this exercise. The k in (xi, xi+k) is currently set to 1 for Cycle 1 in the control window below. The top right button in the control window (currently reading default) can be used to examine preset values for well-known generators. 1 ALG0183 Algorithms and Data Structures 1. a = 3 M = 17 (no constant term) seed = 1 Can you confirm the pattern as provided in lecture material? YES NO (circle one) 2. a = 5 M = 17 (no constant term) seed = 1 Can you confirm the pattern as provided in lecture material? YES NO (circle one) 3. a = 43 M = 2048 (no constant term) seed = 1 For the following number of dots, indicate whether you think the output looks random by ticking √ (random) or crossing X (not random). 35 200 2048 4. a = 45 M = 2048 (no constant term) seed = 1 For the following number of dots, indicate whether you think the output looks random by ticking √ (random) or crossing X (not random). 35 200 2048 What is the smallest number of dots that need to be displayed to show the last plot? ____ 5. a = 75 M = 6553 (no constant term) seed = 1 (Sinclair ZX Spectrum) For the following number of dots, indicate whether you think the output looks random by ticking √ (random) or crossing X (not random). 300 1200 5000 6. Select the preset Grogono generator. Display 20,000 dots with a seed = 1. For the following values of k, indicate whether you think the output looks random by ticking √ (random) or crossing X (random). k=1 k = 14 k = 38 7. Select the preset Park/Miller generator. Display 1,000,000 dots with a seed = 1. Vary k. What value of k results in the generator output not looking random? ____ 2 ALG0183 Algorithms and Data Structures In a sequence of N random numbers: The mean number of runs M is approximated by: (2N-1)/3 (= 15.66 for N = 24) The variance of the number of runs V is approximated by: (16N-29)/90 (= 3.94 for N = 24) Part B Download the program RunsTest.java from the ALG0183 website. Compile and run it. The program should produce a list of 500 calculations of the number of runs in a sequence of 24 randomly generated numbers. Copy and paste this data into Excel. Using Data Analysis in Excel, calculate the descriptive statistics. Make a note of them below: Mean Variance Standard Deviation Minimum Maximum Do the mean and variance match the values of 15.66 and 3.94 obtained from the using the formulae? If not, why not? Write your answer(s) below. Write your own program to produce a list of 500 calculations of the number of runs in a sequence of 24 randomly generated numbers. The sequences should be random permutations of the base sequence {1..24}. You may or may not make use of Weiss´s Random.java. Copy and paste the output data into Excel. Using Data Analysis in Excel, calculate the descriptive statistics. Make a note of them below: Mean Variance Standard Deviation Minimum Maximum Do the mean and variance match the values of 15.66 and 3.94 obtained from the using the formulae? If not, why not? Write your answer(s) below. 3 ALG0183 Algorithms and Data Structures import java.util.Random; public class static static static static static RunsTest { int count,loop; // loop variables Random rand; // random number generator int runs; // number of runs boolean direction; // up run or down run? int first, second, next; // to support calculation public static void main(String[] args) { rand = new Random(); // or new Random(some_seed_value) // A 500 calculations of the number of runs. for (loop = 1; loop <= 500; loop ++) { runs = 1; direction = true; // Runs comprise 24 numbers, each number between 1 and 8. first = rand.nextInt(1000000)+1; //System.out.print(first+" "); second = rand.nextInt(1000000)+1; //System.out.print(second+" "); //Establish if the first run is up or down. if (first > second) direction = false; for (count = 1; count <= 22; count ++) //22 because we have the first two { next = rand.nextInt(1000000)+1; //System.out.print(next +" "); // If a change of direction, a new run. if (direction == true & second > next) { runs++; } if (direction == false & second < next) { runs++; } // Update direction. if (direction == true & second > next) { direction = false; } if (direction == false & second < next) { direction = true; } // Update second second = next; } //System.out.println(); System.out.println(runs); } } } 4