[go: up one dir, main page]

0% found this document useful (0 votes)
19 views9 pages

15 1022q Making Random Numbers s2018

Uploaded by

thomastsettc3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

15 1022q Making Random Numbers s2018

Uploaded by

thomastsettc3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COMP1022Q

Introduction to Computing with Excel VBA

Making Random Numbers

Gibson Lam and David Rossiter


Outcomes
• After completing this presentation, you are
expected to be able to:
1. Use VBA to generate random numbers

COMP1022Q Making Random Numbers Page 2


Random Numbers in VBA
• Random numbers can be generated in VBA using
the Rnd function
• The Rnd function generates a real number smaller
than 1 and bigger than or equal to 0, i.e. [ 0 , 1 )
• For example, the following line of code puts a
random number in a variable MyNumber
MyNumber = Rnd() You can optionally
add the parentheses
in this situation

COMP1022Q Making Random Numbers Page 3


An Example of
Making Random Numbers
• The following example creates a simple math
addition question using random numbers
• Two random integers between the range of
1 to 100 are generated
• The user is then asked
what the sum of these
two numbers is,
like this:

COMP1022Q Making Random Numbers Page 4


Generating Random Integers 1/2
• To generate a random integer in the range of
1 to 100 you will need to do these steps:
1. Generate a number between 0 to 0.99999 using
the Rnd function
' Range of number is [0, 1)
RandomNumber = Rnd()

2. Multiply the generated number by 100


' Range of number is [0, 100)
RandomNumber = Rnd() * 100

COMP1022Q Making Random Numbers Page 5


Generating Random Integers 2/2
3. Truncate the number using the Int function, i.e.
remove the fractional part of the number and
keep the integer (no rounding)
' Range of number is [0, 99]
RandomNumber = Int(Rnd() * 100)

4. Add 1 to the number


' Range of number is [1, 100]
RandomNumber = Int(Rnd() * 100) + 1

COMP1022Q Making Random Numbers Page 6


Randomness of the Rnd Function
• You will find that every time you run your code you will
get the same series of random numbers!
– For example, 1st time you run it:
• The first time your program
asks a random math question:

• Later you run the program 2nd time you run it:
the second time it will ask
the same question again!
• That means any game which uses the random numbers
will be the same every time you play it
• To change this, you need to use Randomize
Simple Math Test 1/2
' Randomize the random number generated by Rnd
Randomize You need to use Randomize to ensure that the
numbers generated are really random every time
' Create the first number in the range 1 to 100
Number1 = Int(Rnd() * 100) + 1

' Create the second number in the range 1 to 100


Number2 = Int(Rnd() * 100) + 1

' Calculate the answer and store it as string


Answer = Number1 + Number2

Continued on the next slide…


Simple Math Test 2/2
Continued from the previous slide…

' Execute the loop at least once


Do
' Ask the question
Loop Guess = InputBox("What is " & Number1 & _
body " + " & Number2 & "?")

' Check the answer at the end of the loop


Loop While Answer <> Guess
Loop condition
MsgBox "Excellent, you have got the " & _
"correct answer!"

You might also like