Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Array

Status
Not open for further replies.

link9

Programmer
Nov 28, 2000
3,387
US
Good morning all --

I need to create a five element array with five random numbers between 1 and 9. The only catch is that there can be no duplicates. I'm getting infinite loops on this, and am hoping that someone can take a look and give my code a tweak in the right direction.

The desired end result: 5 elements with 5 unique random numbers between 1 and 9.
Code:
dim randArray(5), i, j, upperLimit, lowerLimit, good, number

upperLimit = 9
lowerLimit = 1 

for i = 1 to 5
    good = false
    do while not good
        randomize
        number = Int((upperLimit - lowerLimit) * Rnd() + lowerLimit)
        do while j <= 5
            if randArray(j) = number then
                good = false
                exit do
            else
                good = true
            end if
            j = j + 1
        loop
    loop
randArray(i) = number
next

Thanks for any input! :)
Paul Prewett
 
Cash that request -- I forgot to reset the value of j before I started using it on the second iteration of the middle inner loop...

**smacks head**
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top