Random Numbers and an Introduction to Loops

Random Numbers

An important part of the craps or blackjack programs is the use of random numbers.  Computers cannot simply roll dice or spin a spinner.   They must utilize functions that generate sequences of numbers that appear random.   As you learn about these functions you will hopefully understand why your random numbers seem to do strange things.  The usual form for such a function is:

new random number = (last random number * x + y) (mod z)

where x, y and z are integers.  For those of you that are unfamiliar with the mod function, it is like a clock function.  It makes a counting system that wraps around to the first number after the last number is reached (like a clock).  For example in a mod 12 system one would count 0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2, etc..  When you take a number and mod it, you keep subtracting the mod number until it is between 0 and the mod number minus one.  For example 28 mod 12 is (28-12 = 16-12 = 4) 4. 

We can generate a "random" sequence of numbers by picking a starting value, and an x, y, and z.  For example let's start with 10 and choose x=3, y=5 and z=17.  Click the "Next Number" button to generate this sequence of numbers.

Random Number:

We get the following sequence of random numbers: 10, 1, 8, 12, 7, 9, 15, 16, 2, 11, 4, 0, 5, 3, 14, 13, 10, 1, 8 etc.  As you can see this generator will never generate the number 6, and it also repeats after 16 numbers.  See if you can come up with a good random number generator for the numbers 1 to 10.  You can launch a calculator if you need one.   What is the longest sequence you can generate before it starts to repeat?   Why?


Read pages 185-190 in A Computer Science Tapestry for more information.

So far, it has been somewhat difficult to repeat a task a certain number of times or until a specific condition is satisfied.  Your only programming choices were to copy and paste a function several times, or use recursion.   C++ actually has several commands that allow you to do this more easily.  Here you will be introduced to one command called the "while" command. 

The while command let's you perform a certain action or set of actions while a particular condition is satisfied.  The format of a while statement is as follows:

while (condition)
    statements to be repeated here

This would have been useful in the last assignment when we were rolling until the last roll was the same as the current roll.  Pseudocode for such a while statment would look like this:

while(lastroll != thisroll)  // != means not equal

This kind of statement that repeats itself is known as a loop because it loops back again and again.  While loops can be used to repeat a statment or set of statements a given number of times as shown here;

int i;

How many numbers do you think will be printed?  Try this out.   How many were actually printed?  Don't try this, but what do you think will happen if you don't put the i=i+1 statement in there?  The computer will get stuck, because if i never increases, i will always be less than 10.  This is called an infinite loop because the loop will be repeated an infinite number of times.

As an exercise try writing the following two programs:

  1. Have the computer flip two coins 100 times (using a pair of RandInt(1,2) commands).  Have it print out the number of double heads, double tails and one head one tail combinations you get.
  2. Have the computer doll three dice until at least two of them are the same.  Have it print out the number of rolls that this takes.

The next assignment will involve loops so read your book so try these exercises and read your book so that you understand them.

Back to Home Page House3.wmf (25540 bytes)