Question:
Can I get some help with a Q-BASIC program?!?!?!?!?
2011-01-03 18:58:07 UTC
I need to make a game that lets a user select a number between 1 and 100. I have to program the computer to create a random number. If the user guesses the correct number then they win. If they guess wrong, they keep getting tries. I remember for the first part I have to use RANDOMIZE TIMER. I forgot the other part.
Three answers:
2011-01-03 20:08:19 UTC
You're using the QBASIC program that came with MSDOS, right?

Don't you have the help file that accompanies QBASIC? There's an example program for RND that's right there in the RND function reference page.

You should have these 3 files:

QBASIC.EXE

QBASIC.INI

QBASIC.HLP

(You can specify where the QBASIC.HLP file is located in the QBASIC 'Options' menu. Don't put QBASIC in a folder that uses a name that is longer than 8 characters and don't put any space characters in the name. A safe place to put QBASIC is in the C:\Windows" directory.)



Anyway, if you don't have the help file:

The first thing you need to do is to use the RANDOMIZE statement to seed the pseudo-random number generator. (The typical pseudo-random number generator code is usually a simple mathematical algorithm using a 'seed' number and another number.)

You can specify a seed number for RANDOMIZE, but it would be better to use the TIMER function to provide the seed number because the TIMER value will be different with every execution of your program. If your seed number is constant, then your program will always generate the same 'random' numbers on every program execution.



Next, you have to use the RND function, which only generates a floating point random number between 0 and 1. In order to get the RND function to generate a number between the values that you want, you need to 'adjust' the output of the RND function. Usually, you don't want a floating point number, so you need to use the INT statement.



rem x= a random integer between 0 and 7

randomize timer

x=int (rnd(1) * 8)



rem x= a random integer between 1 and 8

randomize timer

x=1 + int (rnd(1) * 8)



rem x= a random integer between 1400 and 2000

randomize timer

x=1400 + int(rnd(1) * 601)



If the random number that you get tends to mostly stay within a small range of numbers, then try using 2 RND lines or develop some kind of algorithm like making even numbers go through an extra RND line (or whatever).
Nick M
2011-01-04 03:14:09 UTC
I don't know Q basic, but I do know the basic logic of the program.



generate random number between 1 and 100



ask user to input number between one and a hundred



do{

input the number



if (number > random) print num was too big



else if(number < random) print number was too small



else print you guessed right!



}



while(number != random)
2011-01-04 03:36:08 UTC
Hey,



Here is what you need.





CLS

RANDOMIZE TIMER



computerValue = INT(RND * 100)

PRINT "Give a number between 0 and 100"

INPUT userValue



IF computerValue - userValue <> 0 THEN

PRINT "False, the correct number was "; computerValue

ELSE

PRINT "You WIN"

END IF







devjobsplus


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...