Ross Sheppard High School
Computer Programming 20
Student Learning Guide
Random Number Programs


  When you have finished this project, you will be able to:
program a Beep
use the random number generator
use multiple forms
know how to make use of user input

describe the following terms:

  • random number
  • randomize
  1. Create a new folder called Random Numbers on your personal drive.

  2. The illustration below shows the visual implementation for the Random Numbers program.



    The Random Number Program

    When the user clicks the Spin Button or selects Spin from the File Menu, a number be randomly selected and printed in the textBox. If the user selects exit, a confirmation dialog box will appear providing a choice of quitting or not. Clicking the About menu item will cause an information screen to appear.

  3. Note: this planner is the starting layout of this program. You will change the design characteristics as you work through this project.

  4. Start a new Standard.exe project in Visual Basic. Save this project as prjRandomNumberProgram and the form as frmRandomNumberProgram in Random Number Program folder.

  5. Remember to add the pseudo-code as comments in your program.

  6. Use the table below to create the Visual Implementation of the Random Number Program:

  7. Objects Table
    Object Property Setting
    Form Name frmRandomNumber
      Caption The Random Number Program
      Icon Choose an icon
      BackColor Choose a color
      StartUpPosition Screen Center
    CommandButton Name btnSpin
      Caption Spin
      Font Choose a font
    TextBox Name txtRandomNumber
      Text Make it blank
      Font Choose a font
      Alignment Center

  8. Use the table below to create the Menu Items for the Random Number Program:

    Menu Items Table
    Caption Name Properties
    &File Name mnuFile
    &Spin Name mnuSpin
    File-sub-menu
    E&xit Name mnuExit
    File-sub-menu
    &About Name mnuAbout

  9. Now that you have completed the Visual Implementation of the Random Number Program, you can begin to create the code.

  10. Create the code for the menu item mnuExit. This will be the same code that you have previously learned.

    Remember, the first code that you must enter for all projects is:



    Note: you will not be reminded to write the General_Declarations() code again. Remember to write this code for each project you complete.

  11. Write the code and the Form for the frmAbout that will appear when the About menu item is clicked.

  12. Next, you will write the code to generate a single random number and display it in the TextBox txtRandomNumber:



    This code goes in the btnSpin_Click() sub-routine.

  13. The purpose of the first line of code is to declare the variable that will be the random number generated by Visual Basic.

  14. The second line of code RANDOMIZE initializes, or starts, the random number generator - the randomizer

  15. The third line of code states that the variable is the random number generated.

  16. The fourth line of code txtRandomNumber.Text = RNum1 places the number generated into the TextBox txtRandomNumber.

  17. The final line of code for this procedure causes the computer to beep.

  18. Save your program and run it. Press the Spin button and a number is placed in the TextBox txtRandomNumber. Press Spin again, and a different number appears.

  19. The next step in this program is to create a "Guess the Number" game.

  20. Create the following and set the appropriate properties: Your Form frmRandomNumber should look like this:


  21. Write the following code for the CommandButton btnSpin_Click() procedure:



  22. As a comment in your code, write an explanation of how the btnSpin_Click() procedure works. Check your description by clicking here.

  23. Ask your teacher to look at your Random Number Program.

  24. Next, you will write code that generates three random numbers in three separate TextBoxes.

  25. Some code and objects will need to be deleted - go ahead and remove the lblGuess, and txtMyNumber.

  26. Add the following objects with the appropriate properties: Your Form should be similar to the one illustrated below:


  27. Re-write the code for the btnSpin:



  28. Save your project and run it. Keep Spinning to ensure the program works and the proper text appears in the txtWinLose TextBox.

  29. Click here to see the marking guide for this project.

  30. When you have finished, send an email to your teacher asking that this project be marked.


© 1998-2000 N.F. Mathew, EdD
File name: Random.htm
Last updated on October 18, 2000