How do I generate a list of random numbers, which are all different?

Question

From:

Martin Allen

Date:

02-01-02 12:39

Subject:

Random number problem

Question:

I am trying to make an array that contains random numbers, but each number has to be different.
I am having difficulty in writing the code. Can anyone help?

Dim D(9) As
Integer, i As Integer, j As
Integer, k As Integer
Do Until i = 9
j = 0
Do Until j = i
k = Int((Rnd * 9 - 0 + 1) + 0)
If D(j) = k
Then
k = Int((Rnd * 9 - 0 + 1) + 0)
Else
D(i) = k
j = j + 1
End If
Loop
i = i + 1
Loop

When your code is run, the same sequence of numbers is returned - 0756318492.
Although all numbers are different, the numbers are always in the same order (unless
that's a coincidence, which I doubt). The above project sorts these numbers into a random order.

The numbers are always in the same order is because you need to declare :
RANDOMIZER TIMER
in the beginning, or else it will always give you the same set of numbers!

Reply:

The statement you need is: Randomize.

Martin Allen 1999 - 2011. Last updated
Tuesday 09 August 2011 08:15:46 PM +0100.