AboutAlex 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++
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