Link to home
Start Free TrialLog in
Avatar of SM17CH
SM17CH

asked on

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
Avatar of abith
abith
Flag of India image

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.
Avatar of cup
cup

Also use randomize to seed the random number generator before you start.

The random number routine is called rnd.
Avatar of ozo
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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
     
     

- 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