So... you have

*k* numbers (in your example,

*k = 200*) and on each of

*n* iterations, you want one of those numbers, so that the same number won't come up again.

If

*k* is always going to be 200, which is not big, then I think what

**counting_pine** suggested is the best approach. Also, as

**deltarho** said, if you were to require that all permutations be possible, you'd need something better than

**RND**.

Now, to be precise, shuffling would be for the case in which

*n = k*, but as long as

*n <= k*, you can do something similar, like this:

Code: Select all

`Dim As Short i, j, n, k = 200`

Dim number(1 To k) As Short, numbers As Short

numbers = k

For i = 1 To k

number(i) = k

Next i

For i = 1 To n

Dim j As Short

r = Int(Rnd * numbers) + 1

Print number(r) '<--- This is your number

'Now remove number from list

For j = r To numbers - 1

number(j) = number(j + 1)

Next j

numbers -= 1

Next i

This will work when

*k* is small. If

*k* is very large, two problems will become important: you'll need a lot of memory and the procedure to remove numbers from the list will be too slow. In those cases, the best you can do (as long as

*n* is not too big) is something like this:

Code: Select all

`#define MAXN 1000`

Dim printed(1 To MAXN) As Short, prnums As Short = 0

Dim As Short i, j

Dim As Short n = 200

Dim As LongInt k = 150000

For i = 1 To n

Dim r As Short

Dim alreadyprinted As Boolean

Do

alreadyprinted = False

r = Int(Rnd * k) + 1

For j = 1 To prnums

If r = printed(j) Then alreadyprinted = True : Exit For

Next j

Loop Until Not alreadyprinted

Print r

'Add r to the list

prnums += 1

printed(prnums) = r

Next i

I can't think of anything better right now. Also notice that, if

*k* is really large, you will absolutely need something better than

**RND** or else some numbers will never come up. And, of course, if both

*n* and

*k* are really big, the cost in time and memory is unescapable.

Another problem with the last algorithm here is that it's skipping items from the

**RND** series. Depending on the method, this might break the even distribution of pseudo-random numbers. In most cases, this won't be a problem, but... it's worth taking into account.

My programming is a little rusty. Haven't done much in a few months. I apologise if there're some bugs there. I think the idea is clear, though.