Qbasic: Generating 20 unique random numbers between 1 and 80 inclusive

Hi,
Im developing a small program using QBASIC.

How can I generate 20 unique random numbers between 1 and 80 inclusive. The numbers must be unique which means if the number 5 is picked it must be unable to come out again. (eg: like lotto or bingo)

Can anyone help me?

Regards
Mitch
LVL 2
SM17CHAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I still have QB4.5 on my puter here...though I literally can't remember the last time I opened it!  =)

Here is a tested QB4.5 example of ozo's algorithm above...

It's messy...because I couldn't remember how to properly return an Array from a Function in QB4.5.
(this code would actually work in VB6 with one tiny change: "Print" becomes "Debug.Print")
RANDOMIZE TIMER
 
DIM i AS INTEGER
DIM count AS INTEGER
DIM index AS INTEGER
DIM tmp AS INTEGER
 
DIM min AS INTEGER
DIM max AS INTEGER
DIM numToSelect AS INTEGER
 
min = 1
max = 80
numToSelect = 20
 
 
' Populate numbers Array
count = max - min + 1
REDIM numbers(1 TO count) AS INTEGER
FOR i = 1 TO count
   numbers(i) = min + (i - 1)
NEXT
 
' Shuffle the numbers Array
FOR i = 1 TO count
   index = INT(RND * count) + 1
   tmp = numbers(i)
   numbers(i) = numbers(index)
   numbers(index) = tmp
NEXT i
 
' Populate the Selections Array
REDIM Selections(1 TO numToSelect) AS INTEGER
FOR i = 1 TO numToSelect
   Selections(i) = numbers(i)
NEXT
  
' Display the selections
FOR i = LBOUND(Selections) TO UBOUND(Selections)
      PRINT i, Selections(i)
NEXT i

Open in new window

0
 
abithCommented:
have an array indexed 1 to 80.

generate random number between 1 to 20 (you can mod for limiting) , say N, check Array[N] is zero, if so change into 1 and increase a count, if not so, generate another random number and follow same above process.

do the same process till you reach 20 in count.
0
 
cupCommented:
Also use randomize to seed the random number generator before you start.

The random number routine is called rnd.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
SunBowCommented:
> generate 20 unique random numbers between 1 and 80

a) Take an array from 1-80 and initialize to zero or false.
    Pull a random number and flag its position in the array as found (set to a one or to true) - only if it was not set, otherwise you cannot count it as a successful toss.
    - this metod wil run for 20 random tosses plus additional tosses for each one that duplicates another

b) Take an array from 1-80 and initialize from one to eighty in sequence
     Toss a random from 1-80
     From the position of the toss read the value of the element of array for the result
     swap into that position of the array the value of the 80th item
     For the next roll of random, only toss for value between 1-79
     - this method always uses exactly 20 random numbers
     
     
0
 
patrickrivaCommented:

- Create an array A of integer with 80 elements

- populate it with numbers between 1 and 80

- for n times (with n great at your pleasure, i.e 200)
  generate two random numbers (i,j) between 1 and 80
  if (i<>j) swap position i and j of A

- take the first 20 numbers of the array

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.