2.4 Random Numbers

To get your programs to do unpredictable things (on purpose that is!) you need to have a way of generating random numbers. Or at least numbers that appear to be random. Any games that roll dice, deal cards or have characters moving around randomly will need to use random numbers. Program that simulate real life situations (eg. to predict traffic patterns or the weather) also need to generate random numbers.

Turing has predefined procedures for generating different types of pseudo-random numbers. Procedures are different than functions. Procedures do not return values. Procedures perform some task. In the case of the random procedures they will assign a random value to a variable that you pass as a parameter. They are pseudo-random numbers since they are not trully random since they are actually from a sequence of numbers that can be calculated. However, the sequence is virtually impossible for an unaided human to predict, so they seem random. In the chapter on writing our own functions we will see how to genereate our own pseudo-random numbers.

randint

The RANDom INTeger procedure takes three parameters. The first parameter is a variable to store the random number in. The range for the random number is indicated by the next two parameters. Here are some examples.

Here is an example program that simulates rolling two dice:

var die1, die2 : int

randint(die1, 1, 6)
randint(die2, 1, 6)

put "You rolled a ", die1, " and a ", die2 

The two randint statements pick random numbers between 1 and 6 to simulate rolling a normal six-sided die. Note that we need variables to store the random numbers. Unlike the functions we saw earlier in the chapter you cannot do things like: put "You rolled a ", randint(die1, 1, 6). There is no value returned that you can print like a function would produce. Each time you run the program you will get different results (well, technically you have a 1/36 chance of getting the same result twice in a row). Here is one possible output:

You rolled a 2 and a 3

Sometimes you need to generate numbers in a given range but you only need every nth value. For example, suppose you wanted to pick a random number from this set {8, 12, 16, ..., 48}, and you wanted to store the result in a variable called number. The first thing you have to do is figure out how many numbers there are in the set. There are 11 numbers in the set. So get a random number in the range {0, 1, 2, ..., 10}. Now you need to adjust those values to fall in the correct range. We are counting by 4 in our set so multiply by 4. This gives a random number in the range {0, 4, 8, ..., 40}. The first number in our set is 8 so add 8. We now get a random number in the required range. Here is a program we could use to verify this:

var number : int

randint(number, 0, 10)
number := number * 4;
number := number + 8;

put "number = ", number 

rand

The rand procedure takes one parameter. It is a real variable that will receive the generated random number. This procedure picks a random real value between 0 and 1 (not including 0 or 1). You can use this value directly as a percentage or you can perform calculations to produce a value in a range that you want. As an example, suppose you were trying to simulate a baseball game. If a player has a .300 batting average then if rand returns a value 0.3 or less you would say they got a hit. Any value above 0.3 would be considered an out. Most programming languages come with a random function similar to this one. Most of the time you won't need this procedure since you will usually find the randint procedure more suitable for your needs. Here is a small example to show the use of the rand procedure.

var percent : real

rand(percent)
put "percentage = ", percent * 100, "%" 

Each time you run this program it will print a different value for x. It will be a real value between 0 and 1.

Exercise 2.4

  1. Write statements that will make the int variable number take on a random value distributed over the given set.
    1. {1, 2, 3, ..., 10}
    2. {1, 2, 3, ..., 54}
    3. {5, 10, 15, ..., 100}
    4. {-5, -4, -3, ..., 5}
    5. {100, 110, 120, ..., 300}
    6. {a, a + b, a + 2b, ..., a + kb} where a, b, k are integers
  2. Write statements that will assign the real variable randReal a random value from the set
    {1.00, 1.25, 1.50, ..., 4.00}.
  3. Write a program that draws red blocks (red space characters) at three random locations on the screen.
  4. Write a program that draws an 8 x 2 rectangle at a random location on the screen in a random colour.