code not working properly - VBA beginner (random numbers generation, no repeats)

M

msburza

I'm trying to write a code that will generate random numbers and no
number appears more than onece.



Code:
--------------------

For I = 0 To 3
For J = 0 To I
Do
n = Int(4 * Rnd) + 1 'random number generated
Array1(I) = n 'random number settled in an array
If (I = I - J) Then 'checking for the same place in array
numsOK = True
Else
If Array1(I) = Array1(I - J) Then 'comparing two different places in an array
numsOK = False
Else
numsOK = True
End If
End If

Loop Until numsOK = True
Next J 'only the last number and the first one always differ, other numbers repeat
 
J

Jim Cone

'Generates five random numbers between 1 and 20
'with no duplicates. (concept stolen from Tom Ogilvy)
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

Sub GetThem()
Dim arrCheck(1 To 20) As Long
Dim arrList(1 To 5) As Long
Dim j As Long
Dim N As Long
Const LNG_PLUG As Long = 999

j = 1
Do While j < 6
'Get a random number
Randomize
N = Int(Rnd * 20 + 1)
'If number unique then add to arrList.
If arrCheck(N) <> LNG_PLUG Then
arrList(j) = N
arrCheck(N) = LNG_PLUG
j = j + 1
End If
Loop

Range("B5:F5").Value = arrList()
End Sub
'--------------


in message
I'm trying to write a code that will generate random numbers and no
number appears more than once.
Code:
--------------------
For I = 0 To 3
For J = 0 To I
Do
n = Int(4 * Rnd) + 1 'random number generated
Array1(I) = n 'random number settled in an array
If (I = I - J) Then 'checking for the same place in array
numsOK = True
Else
If Array1(I) = Array1(I - J) Then 'comparing two different places in an array
numsOK = False
Else
numsOK = True
End If
End If

Loop Until numsOK = True
Next J 'only the last number and the first one always differ, other numbers repeat
 
M

msburza

Thx for help Jim. It's an example of a good code, short and sexy ;)
Best regards
msburza
 
M

msburza

I have made the code working :)

Code
-------------------

Option Explicit
Dim numsStored(5) As Integer
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim numOk As Boolean

Private Sub CommandButton1_Click()

For i = 0 To 5
Do
numRnd
For j = 0 To i

If j = 0 Then
numOk = True
ElseIf numsStored(i) = numsStored(i - j) Then
numOk = False
numRnd
End If
Next j
Loop Until numOk = True
Next i

For i = 0 To 5
Cells(i + 1, 4).Value = numsStored(i)
Next i

End Sub

Private Sub numRnd()

Randomize
n = Int(6 * Rnd) + 1
numsStored(i) = n

End Sub
 
D

Dana DeLouis

Hi. Given the size of your problem, would you just want to do something
like a shuffle?

Dim numsStored(1 To 6) As Long

Sub Demo()
Dim p As Long '(P)ointer
Dim p2 As Long 'Second Pointer
Dim t As Long '(t)empoary

'// Load your 6 numbers...
For p = 1 To 6
numsStored(p) = p
Next p

'// Shuffle
For p = 1 To 6
p2 = Int(6 * Rnd) + 1
' Swap...
t = numsStored(p)
numsStored(p) = numsStored(p2)
numsStored(p2) = t
Next p
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top