You are here:

Qbasic, Quickbasic/Unique random numbers

Advertisement


Question
Hello Alex,
I cannot get this little program to eliminate repeating repeating numbers. Please help. Here it is:

COLOR 15, 1
CLS


'this program displays a list of 56 numbers in the range of 1-21
'the user enters a number in the range of 1-21
'the program tells where each occurrence appears.
'example: 4 1 13 11 20 9 13...
'user enter "13"
'program displays 3 7...
'since 13 is at position 3, 7,etc
DIM somenumber AS INTEGER ' a number to be found in the array below.
DIM somearray(56) AS INTEGER 'integers displayed on screen
INPUT "please type a random-number seed"; seed
RANDOMIZE seed 'initialize the random number generator

FOR ctr = 1 TO 56
   10 somearray(ctr) = CINT(RND * 56 + 1)
   20 IF somearray(ctr) = CINT(RND * 56 + 1) THEN GOTO 10


   PRINT somearray(ctr);
NEXT ctr
COLOR 15, 1


DO
   PRINT
   INPUT "what number do you want to search for?(enter 0 to quit)"; position
   IF position = 0 THEN EXIT DO
   somenumber = CINT(position) 'change it to an integer

   IF somenumber < 0 THEN somenumber = 1 'if it's less than 0, let it =1
   IF somenumber > 56 THEN somenumber = 56 'if it's bigger than 56 let it =56




   PRINT "positions containing"; somenumber; "are:";
   FOR cnt = 1 TO 56
       IF somenumber = somearray(cnt) THEN PRINT cnt;
   NEXT cnt
   PRINT "" 'finish up
LOOP 'until user enters zero




PRINT "finished"

Answer
Hi Emerson,

Looks like there are a few things that we can look at in your code and try and work something out.  First thing I notice is that you're generating random numbers between 1 and 56 rather than 1 and 21.  This definitely isn't the issue, but it's something you may want to address if this is a homework question.

Next thing on the list, your FOR/NEXT loop to initialize your values looks a little off.  To explain what your code is telling the computer:

 * Loop through numbers 1 to 56, and for each, do the following (this part is right):
   1. Assign somearray(ctr) as a random number between 1 and 56 (other than your range, this is right, too)
   2. If the randomly assigned number in the above assignment is also equal to another random number, then repeat the above assignment (this is completely unnecessary)
   3. Print somearray(ctr)


I'm not sure what you're trying to do with that IF statement in your FOR/NEXT loop, but it won't do anything unless you happen to randomly generate a number twice in a row (very unlikely), and if that does happen, then you tell QBasic to grab another random number.

Now this next bit is going to be where you have your issue:

   INPUT "what number do you want to search for?(enter 0 to quit)"; position
   IF position = 0 THEN EXIT DO
   somenumber = CINT(position) 'change it to an integer


There are a few problems I can see semantically.  For the sake of sanity, since you are asking for a number, we should use your somenumber variable.  Because it's already declared as an integer, QBasic will automatically make sure it's a proper integer value.  So here is that corrected chunk of code:

   INPUT "what number do you want to search for?(enter 0 to quit)"; somenumber
   IF somenumber= 0 THEN EXIT DO



The rest of your code just requires you making sure you're using the proper range (whether or not it's 1-21 or 1-56).  You may want to add some constant variables to the top of your code to easily switch it:

CONST MIN_NUMBER% = 1
CONST MAX_NUMBER% = 56

''And for your randomize statements:
somearray(ctr) = CINT(RND * MAX_NUMBER% + MIN_NUMBER%)
''And IF statements:
IF somenumber < MIN_NUMBER% THEN somenumber = MIN_NUMBER%
IF somenumber > MAX_NUMBER% THEN somenumber = MAX_NUMBER%


On that note, your IF statement in your DO/LOOP is wrong- - it should be IF somenumber < 1, not 0


I hope that helps,
-Alex

Qbasic, Quickbasic

All Answers


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2016 About.com. All rights reserved.