You are here:

Qbasic, Quickbasic/unique random numbers

Advertisement


Question
Hi Alex, it's me again. With this little program I can get 56 different numbers and then count where they appear in the list. Just would be easier to have the computer point them out. Please tell me what you think. Thanks.

CLS

COLOR 15, 1


1 REM lotto

20 DIM n(56)
21 INPUT "please type a random-number seed"; seed
25 RANDOMIZE seed 'initialize random number seed
30 FOR i = 1 TO 56
   40 a = INT(RND(1) * 56) + 1

   50 FOR j = 1 TO 56
       60 IF n(j) = a THEN GOTO 40
   70 NEXT j
   80 n(i) = a
90 NEXT i
100 FOR i = 1 TO 56

   110 PRINT n(i);
120 NEXT i
125 PRINT

Answer
Hi Emerson,

So, judging by your loop, you want the numbers 1 to 56, with no repeating numbers, but in a random order?  If that's the case, this is how I would do it:

DIM n(1 TO 56) AS INTEGER

'' Get the numbers 1 to 56 in an array - this way, we will always get all the numbers
'' with no repeats
FOR i% = 1 TO 56
  n(i%) = i%
NEXT i%

'' Get a random seed
INPUT "please type a random-number seed"; seed
RANDOMIZE seed

'' Randomize the order

CONST RANDOMNESS% = 400 '' The higher this number, the more scrambled the array
                        '' should become
FOR r% = 1 TO RANDOMNESS
  index1% = INT(RND * 56) + 1
  index2% = INT(RND * 56) + 1
  SWAP n(index1%), n(index2%)
NEXT r%


And to sort out where in the array a number exists:

index% = -1 '' Since we know that -1 will never be a valid index (the array
                 '' indices are only valid between 1 and 56), we can say that
                 '' index% can only equal -1 if the number wasn't found

WHILE index% = -1
  INPUT "number (between 1 and 56): ", number%
  FOR j% = 1 TO 56
    IF number% = n(j%) THEN
      index% = j%
      PRINT number%; " was found at position "; index%
      '' If you only want the first position (presuming you do not want repeating
      '' numbers), then you could uncomment the next line
      'EXIT FOR
    END IF
  NEXT j%

  IF index% = -1 THEN '' Only if the number hasn't been found
    PRINT "number out of range, pick a different number!"
  END IF
WEND


I hope that points you in the right direction,
-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.