Chris Pollett>Old Classes>PIC 10a, Spring 2000>Hw2

Spring 2000 PIC 10a HW #2

Due date: April 25, 10:30p.m.

For this assignment the code you submit should be in a file named RandomDrawer.cpp.

Purpose: The purpose of this assignment is to get students familiar with arithmetic expressions, while loops, functions, functions prototypes, and simple random number generators.

Description: The ability to generate a good sequence of "random" numbers is an important problem in computer science. Besides being useful for computer games, it has uses in randomized algorithms, cryptography and numerical analysis. One simple type of random number generator is the so-called linear congruential generator. In this kind of generator, the sequence of random numbers (not really random but supposed to look that way) is generated in the following way: (1) The first element (s1) of the sequence is an integer seed value supplied by the user. (2) The next number (s2) in the sequence is given by s2 = (a*s1 +r)%M where a, r, M are fixed for the given generator. Define R(s) := (a*s + r)%M (3) Thereafter, the (n+1)st element of the sequence is defined to be R(sn).

As an example, if s1=3, a=5, r=7, M=11 then s1=3, s2=0, s3=7, s4=9,... The random numbers in the sequence (after maybe the first number) will always be less than M.

For this assignment, you will write a program which plots the sequence of values generated by such a generator so that you get some intuition for how random this sequence is or is not. Your program should first query the user for values M, a, r, initial seed, and a number of rows to draw. (Your program is not required to do any error checking to make sure these are all reasonable integers.) The values M, a, and r are usually referred to as the modulus, scalar multiplier, and offset. Your program should then plot out that many rows of the generator. For example, here's what my program did:


Welcome to the linear congruential
random number plotter.

Please enter the data asked for below to see a plot.
All inputs should be positive integers.

Enter a modulus:
11

Enter scalar:
5

Enter offset:
7

Enter start seed:
3

Enter number of rows:
15
   *
*
       *
         *
        *
   *
*
       *
         *
        *
   *
*
       *
         *
        *

Notice in the first row the * is three spaces over. This correspond to the value of s1. The second row the * is zero spaces over corresponding to s2, etc. After plotting a figure such as above your program should stop. Your program is required to use two auxiliary functions with prototypes:


void RandomPlotter( int modulus, int scalar, int offset, int seed,
		int numberOfRows);

void PrintRow(int columns);

The first function takes parameters modulus, scalar, offset, seed, and numberOfRows and plots that many rows of the generator. The second function takes the parameter column and prints that many spaces to the screen followed by a *. Both of these functions should use while to do their looping. The actual inputs from the user should be gotten in the main function. Once you've written your program try experimenting with it to see how random these generators really are. Have fun!

Homework 2 FAQ.

Solution set.