AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About 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, c/c++, python, 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
Hobby programming since 2000

I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > Unique number generating

Qbasic, Quickbasic - Unique number generating


Expert: Alex Barry - 3/10/2009

Question
hi
I read your past answers but none of them seemed to help me with my problem.(this is the first time i'm using Qbasic so i have a very basic knowledge of Qbasic )i have this school assignment to make a qbasic program for the lottery.  we have to draw 6 random numbers between 1 and 44. i can do that but some of the numbers keep repeating and they need to be unique. here is my code:

REM This is a random number generator between number 1 and 44.
REM The numbers cannot be the same.
DIM number AS INTEGER
DIM firstname, reply AS STRING
RANDOMIZE TIMER
CONST maxnumber = 44
CLS
COLOR 14
PRINT " Would you like to try your luck with this program? (yes/no); reply
INPUT reply
       IF reply = "no" THEN
       COLOR 13
       PRINT "Maybe next time eh?"
       END IF
     IF reply = "yes" THEN
    DO
     COLOR 9
      PRINT " Well here are your numbers..."
      FOR counter = 1 TO 6
          COLOR 11
          number = INT (RND * maxnumber) + 1
          PRINT number
      NEXT counter
          PRINT "Good luck!"
          INPUT " Would you like to have another try?(yes/no); reply
      LOOP UNTIL reply = "no"
           END IF
           END
END

i am able to make the number come up on the screen but some times they turn out to be the same thing. for example:
6
34
2
34
12
27
the 34 repeated twice. i have been trying for weeks to figure this out with my friend and the teacher is always away. i tried using data operators but i don't think i am using them correctly. can you please help me?

thankyou,
thomas

Answer
Hi, Thomas,

To solve this, the easiest way is to make an array that stores all the numbers you draw.

If you are drawing six numbers, you'll want an array that holds 6 integers:

DIM DrawnNumbers(1 TO 6) AS INTEGER

     FOR counter = 1 TO 6
         COLOR 11
         number = INT (RND * maxnumber) + 1
         PRINT number
     NEXT counter

Now, to see if a number has already been drawn, you'd do this:

DIM i AS INTEGER

FOR i = 1 TO counter-1
 IF DrawnNumbers( i ) = number THEN
   REM - This number has already been drawn.
 END IF
NEXT i

Now, when you find a number already guessed, I suggest using a GOTO jump back to where a random number is guessed.

If you need help piecing this together, show me what you have and I'll help you out,
-Alex

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.