|
Dictionaryを使わない版も示しておきましょう。
たぶん、こちらのほうが理解し易いでしょうし、
追加の加工もし易いはずです。
Option Explicit
Sub test() ' 繰り返し判定版
Dim i As Long
Dim j As Long
Dim n As Long
Dim m As Long
Dim a As Long
Dim b As Long
Dim k As Long
Dim seq As Long
Range("A1").CurrentRegion.Clear
For k = 1 To 50
'最初の二つをランダムにセット(そのままにしてあります)
For i = 1 To 2
j = Int(((4 - 1 + 1) * Rnd) + 1)
If i = 2 Then
Do While Cells(i - 1, k).Value = j
j = Int(((4 - 1 + 1) * Rnd) + 1)
Loop
End If
Cells(i, k).Value = j
Next i
'使用する系列
seq = WorksheetFunction.RandBetween(1, 2)
For n = 3 To 100
a = Cells(n - 2, k).Value
b = Cells(n - 1, k).Value
' 特定の組み合わせのとき、系列を再検討
If (a = 4 And b = 3) Or (a = 1 And b = 4) Or (a = 2 And b = 1) Then
seq = whichSequence(0.85)
End If
'次の数値を得る
If seq = 1 Then
Cells(n, k).Value = nextByA(a, b)
Else
Cells(n, k).Value = nextByB(a, b)
Cells(n, k).Interior.Color = 15773696 '背景色をつけました
End If
Next n
Next k
End Sub
Function whichSequence(v As Double) As Long
If Rnd < v Then
whichSequence = 1
Else
whichSequence = 2
End If
End Function
Function nextByA(a As Long, b As Long) As Long
Select Case True
Case a = 3 And b = 2: nextByA = 4
Case a = 3 And b = 4: nextByA = 2
Case a = 4 And b = 1: nextByA = 3
Case a = 1 And b = 2: nextByA = 1
Case a = 2 And b = 4: nextByA = 1
Case a = 4 And b = 3: nextByA = 2
Case a = 3 And b = 1: nextByA = 2
Case a = 1 And b = 4: nextByA = 3
Case a = 4 And b = 2: nextByA = 3
Case a = 2 And b = 1: nextByA = 4
Case a = 1 And b = 3: nextByA = 4
Case a = 2 And b = 3: nextByA = 1 '■追加
End Select
End Function
Function nextByB(a As Long, b As Long) As Long
Select Case True
Case a = 3 And b = 2: nextByB = 3
Case a = 3 And b = 4: nextByB = 1
Case a = 4 And b = 1: nextByB = 2
Case a = 1 And b = 2: nextByB = 4
Case a = 2 And b = 4: nextByB = 3
Case a = 4 And b = 3: nextByB = 1
Case a = 3 And b = 1: nextByB = 4
Case a = 1 And b = 4: nextByB = 2
Case a = 4 And b = 2: nextByB = 1
Case a = 2 And b = 1: nextByB = 3
Case a = 2 And b = 3: nextByB = 4 '■修正
Case a = 1 And b = 3: nextByB = 2 '■追加
End Select
End Function
|
|