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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
cupCommented:
Also use randomize to seed the random number generator before you start.

The random number routine is called rnd.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
     
     
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.