CS 227                                                RWN
Program #4 - Random Number Generation
 Why?  

You're going to write, test and use your own random number generator. You may
use any language, although the rest of the description presumes Java. If you
use some other language still try to follow the design suggested below. 


The basic method will be the LCM (Linear Congruential Method)

  x0 = seed
  x1 = (a*x0 + c) mod m
  x2 = (a*x1 + c) mod m
  etc.
  
a is called the multiplier
c is called the increment
m is called the modulus

Max Period Theorem
The maximum period m will be obtained iff
i)   c and m are relatively prime (have no common divisors)
ii)  if a prime p divides m (evenly) then p also divides a-1 (evenly)
iii) if 4 divides m then 4 divides a-1

Suggestions:

  Often one takes m = 2^n or 10^n or some large prime, with m being
  close to the largest int (or long) allowable
  
  a often ends in 21, is large, one digit or so less than m. 

  You can use ints or longs but do not use any BIG INT package.
  
  You might get and test your m, a and c using Mathematica (rather than Java.)   
  Here's some Mathematica examples that might be useful 
  Mathematica Examples  
  
  Here are some hints for working with large integers. On Large Integers

Your assignment:

1. Write a RNG.
   In Java, put your RNG and everything related into a self-contained
   class, say Ran, or Rand, or Random.
   
   Class design questions:
   
   
In the main include calls for:

2. A Chi-squared test for equidistributivity, with k buckets (i.e. classes),
   for k = 2, 6, 10 and 52.  Chi2  
   
3. Report your results, esp. on part 2 in a README file.

4. Do at least one of the following options (and report on it in the README.)
   
   a.(Optional) Include the test of the Max Period Th. above in your package.

   b.(Optional) Save the last value of xn in a file and allow for its
   use for the seed for the next time the program is run.
   
   c. (Optional) Build the Chi^2 cutoffs into your program.

   d.(Optional) Include a normal random number generator. See 
      N(m,s)  
     
   e. (Optional) Compare the methods with m = 2^n and m = large prime for time and
      equidistibutivity.
      
   f. (Optional) There are two ways to get a random integer, say between 0 and n-1. One way
      is simply to mod the one on the whole range 0..m-1 by n. Another is to
      get a raadom real r, 0 <= r < 1. Multiply that by n to get a real x,
      0<= x < n; and then truncate that to get the integer on {0,1,..,n-1}.
      Compare these for timing and equidistributivity for small n. (Several
      years ago the built-in C++ rng would fail if the first method were used.
      
   g. (Optional) Generate random triangulations, binary search trees or MCM pairings at
      random (employing the recurrence relation).
      
   h. (Optional) Estimate the number of solutions to the n-queens problem (or some other
      backtracking problem) as in the book.  
          
   i.(Optional) Other enhancements.

Note: All your main should do is initiate and run the tests. The
algorthms should all be in the class file(s) and easily portable to another 
program such as mine or future ones of yours. In fact, for the remainder of 
the term in this class (i.e. course) you should be using your RNG rather 
than one provided in some other package.

Deliverables: by HSP, in a directory named PROG4. Include a description of
all your work in a README file. In particular, include

  

Cooperation: You may speak to others but do your own work. Acknowledge any assistance received.
See the instructor if you've any questions.

Deadline: Wed 2/8. (Let me know should this creates a hardship.)
Get an early start.

What counts?Class design, validity of the tests, readability. Of course what you 
do counts too, but how well you do what you do is more important, so take care of the
basics first.


Collected links Why? Mathematica Examples On Large Integers Chi2 N{m,s) Quotes: "Come and let us cast lots." - Jonah 1:7 "I cannot believe that God plays dice with the cosmos." - Albert Einstein, 1954 "God not only plays dice, He also sometimes throws the dice where they cannot be seen." - Steven Hawking, 1975