Home        Math Games        Word Games        Daily        Reference        Miscellaneous        Junior!        Problems



Beginner Programming Tips

Beginner Programming Tips and Tricks

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.

Index Previous    Next    Teachers    About   

AddThis Social Bookmark Button     Link To This Page

Random Numbers Generators

The first programs I ever wrote were games. They were silly games, and nobody but me wanted to play them, but they were definitely good programming experience. Of course, if you are writing a game program, you need to generate random numbers, otherwise the game gets quite tedious, doing the same thing over and over and over...

So I very quickly discovered this little snippet of code, and used it extensively without ever bothering to look at what was actually happening "behind the scenes".

Dim MyRandomNumber As Integer

'Pick A Random Number Between 1 and 10 Inclusive
MyRandomNumber = Int(Rnd * 10) + 1 

Here is what's happening. "Rnd" is the actual Random Number Generator. What does it generate? It generates a Random number between 0 and 1, possibly including 0, but never including 1. So when you multiply it by ten, you get a number which is somewhere between zero and ten. Possibly including 0, but never including ten.

And here comes the handy math function "Int", which takes the integer part of a decimal value. For example, the integer part of 2.3 is 2. The integer part of 6.9 is 6.

So when you take the integer part of your random number times ten, you get any integer from zero to nine, but not ten. Thus, to get a random number between one and ten, you just need to add one to your random number.

Now take a look at this piece of code:

Dim MyRandomNumber As Integer

'Pick A Random Number Between 5 and 9 Inclusive
MyRandomNumber = Int(Rnd * 5) + 5 

Do you see what's happening here? If you are looking for a random number between five and ten, you have five possible values (5, 6, 7, 8, and 9). So you have to multiply the random generator by five. Of course, that just gives you the following values: 0, 1, 2, 3, or 4. So you need to add five to get the values you want.

Want to try another one? Let's say you want the computer to pick a random multiple of 5 between 0 and 50 inclusive. Here's the code for it; see if you can figure out why it works. And when you've done that, try some of the Other Scenarios listed below.

Dim MyRandomNumber As Integer

'Pick A Random Multiple of 5, between 0 and 50
MyRandomNumber = 5* Int(Rnd * 11)

Other Scenarios

  1. What numbers could be generated by this line of code: MyRandomNumber = Int(Rnd * 4) + 3
  2. What numbers could be generated by this line of code: MyRandomNumber = 2 * Int(Rnd * 5) - 2
  3. What numbers could be generated by this line of code: MyRandomNumber = 5 * Int(Rnd * 3)
  4. Write a line of code to generate a random number between one and fifty, inclusive.
  5. Write a line of code to generate a random number between seven and twenty, inclusive.
  6. Write a line of code to generate an even random number between two and ten, inclusive.

Index Previous    Next    Teachers    About   

AddThis Social Bookmark Button     Link To This Page
"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 


Want To Try Something Completely Different?

The Treasure Hunt

Pirate's Map

Easter Eggs



Site Features

Word Games

Word Games

Math Games

Math Games

Daily Puzzles

Daily Puzzles

Brainfood

Brainfood

Math Problems

Problems

Miscellaneous

Miscellaneous


 
Search For More Resources

Search For More Educational Resources

Find more educational, problem solving, and puzzle resources using the Google safe-search below.

Google

Member Features
Login
 

Picture Word
Four-Scramble
Word Search
Blackberry Game
Telephone Game
Cheater Hangman
Word Grid
Secret Word
Scrambled Word
One of These
Hangman
 

Entrapment
Adders!
Side By Side
One To Ten
Sub Triangles
Magical Squares
Math Scramble
Secret Number
Secret Number 2
Fractional Hi Lo
Concentration
Monty Hall Game
 

Trio Match
Treasure Hunt
Pirate's Map
Fizziks Tilt
Zero Gravity
Easter Egg Hunt
Quad Puzzle
Tic Tac Toe
Rotating Block
 

Codes
Programming
Search It Out!
 
Secret Word
Scrambled Word
Secret Number
Word Grid
 
Brainfood
Math HS
Maine Page
Calculus Page
 
Contact
About
Related Sites
Link to TPS
 

Bookmarking and Linking
Bookmark/Link


Home        Math Games        Word Games        Daily        Reference        Miscellaneous        Junior!        Problems