I'm trying to write a code that will generate random numbers and no number appears more than onece.
Next I Plz help me and tell why the program isnt working. I have no idea, how to make it work.![]()
Please Login or Register to view this content.
I'm trying to write a code that will generate random numbers and no number appears more than onece.
Next I Plz help me and tell why the program isnt working. I have no idea, how to make it work.![]()
Please Login or Register to view this content.
'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
'--------------
"msburza" wrote 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
--------------------
Next I Plz help me and tell why the program isnt working. I have no
idea, how to make it work.
--
msburza
Thx for help Jim. It's an example of a good code, short and ****
Best regards
msburza
I have made the code working
![]()
Please Login or Register to view this content.
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
--
HTH. :>)
Dana DeLouis
Windows XP, Office 2003
"msburza" <msburza.27nifb_1147364401.9108@excelforum-nospam.com> wrote in
message news:msburza.27nifb_1147364401.9108@excelforum-nospam.com...
>
> 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
>
> --------------------
>
>
> --
> msburza
> ------------------------------------------------------------------------
> msburza's Profile:
> http://www.excelforum.com/member.php...o&userid=34222
> View this thread: http://www.excelforum.com/showthread...hreadid=539800
>
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks