Chapter 11 Random Numbers

A very useful function that is available in most programming languages is one that generates random numbers. It must be noted however, that is only creates pseudo-random numbers and cannot be relied upon to give fair results.

The function Rnd will generate a randomly single number between 0 and 1. To create and integer number, the value must be multiplied by an integer and convert the result to an integer. .

Num = Int(6*Rnd)+1 will produce a random numbers between 1 and 6.

Problem 11

A primary school teacher needs a program that will test a students addition skills. The program generates 4 simple addition problems and determines the number of correct answers.

Planning

The main plan for this as follows. Data is obtained using an input box and results shown by a message box. Labels are used to display the number of correct answers and the number of questions asked.

Pseudocode

Set Correct To 0
For Count <- 1 To 4
  Set Number1 To Int(10 * Rnd) + 1
  Set Number2 To Int(10 * Rnd) + 1
  Set Sum To Number1 + Number2
  Get Answer 
  If Answer = Sum Then
    Write well done
    Set Correct To Correct + 1
    Write Number correct
  Else
    Write Wrong
  End If
  Write Number Completed
Next Count

Code

Note: The function Randomize is used in one of the first lines of the program to enable the program to start with a different random number each time the program is run.

The important controls are the command button (CmdAdd)to start the Set of questions and two labels that update the number correct (LblCorrect) and the number of questions given (LblCompleted). The rest of the design should be straight forward.

Private Sub CmdAdd_Click()
  Const max = 4
  Dim Number1 as Integer, Number2 as Integer
  Dim Sum as Integer, Answer as Integer
  Dim Correct As Integer, Count As Integer
dim txtanswer as string
  Randomize
  Correct = 0
  LblCorrect.text = "Number correct is " & Correct & "/" & max
  LblCompleted.text = "Number Completed is 0/" & max
  For Count = 1 To max
     Number1 = Int(10 * Rnd) + 1
    Number2 = Int(10 * Rnd) + 1
    Sum = Number1 + Number2
    txtAnswer = InputBox(Number1 & " + " & Number2 & " =", "Maths Adder", 0, 2000, 3000)
    answer = CInt(txtAnswer)
If Answer = Sum Then
      MsgBox ("well done")
      Correct = Correct + 1
      LblCorrect.text = "Number correct is " & Correct & "/" & max
    Else
      MsgBox ("Wrong - answer is " & Sum)
    End If
    LblCompleted.text = "Number Completed is " & Count & "/" & max
  Next Count
End Sub

Exercises Chapter 11

  1. A program is needed to simulate tossing a die 100 times and keeping track of the results. Display the final results in a text box.
  2. Modify the program above to show the results of tossing two dice and adding the results. Again, display the results in a text box.
  3. The game of two up requires the tossing of two coins. Write a program that will simulate tossing two coins 100 times and display the results in a text box. As an extra challange, what is the longest run of 2 Heads and 2 Tails encountered in the 100 tosses.