Generating Random Questions and Operators

You have already seen how to generate random numbers in VBA. The Math Game program requires the operands for each question to be randomly generated and the operator is randomly generated if the user selects the proper Option Button control (the button labeled "Any," see Figure 4.7).

The GetOperatorType() procedure tests the Value property of the Option Button controls to see which operator has been selected by the user. If the user selects the option "Any," then the GetRandomOperator() procedure is called to generate a random number between 1 and 4. This procedure writes the operator to the merged cells I defined with the name Operator, and is only used when the operator is randomly chosen by the program. You should recall that when a user selects a specific operator, the Click() event procedure of the Option Button control writes that operator to the Operator range.

Private Sub GetOperatorType() 'Gets the operator selected by the user. If optAdd.Value = True Then opType = 1 If optSubtract.Value = True Then opType = 2 If optMultiply.Value = True Then opType = 3 If optDivide.Value = True Then opType = 4 If optAny.Value = True Then GetRandomOperator End Sub

Private Sub GetRandomOperator()

'Randomly selects the type of operator for the question. Randomize opType = Int(4 * Rnd) + 1 Select Case opType Case Is = 1

Range("Operator").Value = "+" Case Is = 2

Range("Operator").Value = "-" Case Is = 3

Range("Operator").Value = "x" Case Is = 4

Range("Operator").Value = "/" Case Else

Range("Operator").Value = "+" End Select End Sub

A question's operands are written to the appropriate cell locations (F8:G9, defined name Left-Operand and I8:I9, defined name RightOperand) with the GetOperands() sub procedure that calls the GetRandomNumber() function procedure in order to generate and return the operands randomly. If the mathematical operation is division, the GetRandomNumber() function uses a loop that will continue to iterate until a second operand is found that results in a non-fractional answer. The VBA operator Mod is used to test the two random numbers for a remainder of zero.

The GetOperands() sub procedure is called from the Click() event of the Command Button control cmdBegin and the Change() event of the worksheet (listed later).

Private Sub GetOperands()

'Adds randomly choosen operands to the worksheet. Dim rightOperand As Integer rightOperand = GetRandomNumber(l) Range("RightOperand").Value = rightOperand Range("LeftOperand").Value = GetRandomNumber(rightOperand) End Sub

Private Function GetRandomNumber(divisibleBy As Integer) As Integer 'Generates the random numbers for the operands. Dim ranNum As Integer Const upperLimit = 10

Randomize

'Generate the random integer. If operation is division, 'then make sure the two operands are evenly divisible.

Do ranNum = Int(upperLimit * Rnd) + 1 Loop Until ((opType <> 4) Or (ranNum Mod divisibleBy = 0))

GetRandomNumber = ranNum End Function

The game is now ready for the user to enter his or her answer, so the timer must start counting down.

Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook