Saturday, 2024-04-27, 1:05 PM

Welcome Guest | RSS

MainRegistrationLogin
Qbasic Home

Catalog categories
Qbasic Tutorial [31]

Rate my Site
Rate JPSor Site
Total of answers: 168

Main » Articles » Qbasic Tutorial

Chapter 11 - Qbasic Tutorial

Random numbers are numbers that you can't predict. Flipping a coin or rolling dice will give you a random number. Random numbers are very important in games and in some kinds of Math. Computers can generate random numbers pretty well. QBASIC's RND function provides random numbers that we can use.

RND

RND is a special function that gives us a random number between 0 and 1. We can use this in games to make things interesting. RND is perfect for rolling dice or flipping a coin. First let's see RND in action:

 CLS
PRINT RND
PRINT RND

This program will print RND twice. Notice that you'll get two numbers that appear to be unpredictable and random. But, try running the program again. You'll get the same "random" numbers. This means your games would always be the same each time the user runs them. Fortunately, there's a way to fix this.

RANDOMIZE TIMER

Using RANDOMIZE TIMER will make sure the random numbers you get are different each time you run. Try this:

 CLS
RANDOMIZE TIMER
PRINT RND
PRINT RND

Useful Random Numbers

Random numbers between 0 and 1 aren't really very useful. What you will need for a game might be a random number between 1 and 6, like when you roll dice. To get something more useful, we'll use math. Fortunately, computers are very good at math.

There are two problems we must solve to get the results we want. First, the range of random numbers has to be expanded from 0 through 1 to 1 through 6. That's easily done like this:

 CLS
RANDOMIZE TIMER
PRINT RND * 6 + 1
PRINT RND * 6 + 1

By multiplying by 6, we increase the range to 0 through 5. By adding 1 we shift the range up to 1 through 6. However, there's still a problem. All that decimal stuff. QBASIC's INT function can be used to convert a decimal number to an integer (a number without a decimal).

 CLS
RANDOMIZE TIMER
PRINT INT(RND * 6 + 1)
PRINT INT(RND * 6 + 1)

Roll the Dice

Here's a program that rolls two dice and prints the value of each. The variables Die1 and Die2 are used to hold the values of each die before printing. In a real game, Die1 and Die2 would be used in some clever way to change the outcome of the game.

 CLS
RANDOMIZE TIMER
INPUT "Press ENTER to roll dice...", A$
PRINT
Die1 = INT(RND * 6 + 1)
Die2 = INT(RND * 6 + 1)
PRINT "Die 1: "; Die1
PRINT "Die 2: "; Die2

PRINT By Itself

Note that in the last program there was a PRINT on a line by itself. Did you see what it did? It simply printed a blank line on the screen. This can be useful for making the output from your program look nicer.



Source: http://jpsor.ucoz.com
Category: Qbasic Tutorial | Added by: JPSor (2009-02-26) | Author: JPSor
Views: 4310 | Rating: 0.0/0 |
Login

Qbasic Clock

Search

Visit my Friends

Who's Online

Total online: 1
Guests: 1
Resgistered: 0


Copyright MyCorp © 2024