因此,以下代码应作为您要实现的目标的基础 - 请注意,目前这会在活动表中产生热度,参赛者按数字分配;需要进一步的工作以适应您的工作表
它会创建一个参赛者数组,然后按计算量顺序“移动”数组中的元素,以便在全部热量中,每个元素应该在每个“热量”数组的前半部分和后半部分中均等地出现。然后将数组分成两半,每一半都是随机的。
因此,它应该生成一个随机配对,其中每个参赛者在右侧或左侧车道上均等...
Sub GenerateHeatData()
Dim Contestants As Long: Contestants = 16
Dim Heats As Long: Heats = 6
Dim CycleLength As Long: CycleLength = WorksheetFunction.Ceiling(Contestants / Heats, 1)
Dim i As Long, j As Long, Arr() As Variant, Left() As Variant, Right() As Variant
Dim BaseArray() As Variant
ReDim BaseArray(Contestants - 1)
For i = 0 To UBound(BaseArray)
BaseArray(i) = i + 1
Next i
Dim BaseHeatArray() As Variant
ReDim BaseHeatArray(Heats - 1)
For i = 0 To UBound(BaseHeatArray)
BaseHeatArray(i) = i + 1
Next i
Call RandomiseArray(BaseHeatArray)
For i = 0 To Heats - 1
Arr = RightShiftArray(BaseArray, CycleLength * CLng(BaseHeatArray(i)))
Left = ExtractArray(Arr, 0, WorksheetFunction.Ceiling(UBound(Arr) / 2, 1))
Right = ExtractArray(Arr, UBound(Left) + 1, UBound(Arr) - UBound(Left))
Call RandomiseArray(Left)
Call RandomiseArray(Right)
For j = 0 To UBound(Left)
ThisWorkbook.ActiveSheet.Cells(j + 1, 3 * i + 1) = CLng(Left(j))
Next j
For j = 0 To UBound(Right)
ThisWorkbook.ActiveSheet.Cells(j + 1, 3 * i + 2) = CLng(Right(j))
Next j
Next i
End Sub
Function RightShiftArray(InArray() As Variant, Shift As Long) As Variant()
Shift = Shift Mod (UBound(InArray) + 1)
If Shift < 1 Then Shift = Shift + UBound(InArray)
Dim TempArr() As Variant: ReDim TempArr(Shift - 1)
Dim i As Long, Arr() As Variant
ReDim Arr(LBound(InArray) To UBound(InArray))
For i = LBound(InArray) To UBound(InArray)
Arr(i) = InArray(i)
Next i
For i = 0 To UBound(TempArr)
TempArr(i) = Arr(UBound(Arr) - Shift + i + 1)
Next i
For i = 0 To UBound(Arr) - Shift
Arr(UBound(Arr) - i) = Arr(UBound(Arr) - i - Shift)
Next i
For i = 0 To UBound(TempArr)
Arr(i) = TempArr(i)
Next i
RightShiftArray = Arr
End Function
Function RandomiseArray(Arr() As Variant)
Dim i As Long, j As Long
Dim Temp As Variant
Randomize
For i = LBound(Arr) To UBound(Arr)
j = CLng(((UBound(Arr) - i) * Rnd) + i)
If i <> j Then
Temp = Arr(i)
Arr(i) = Arr(j)
Arr(j) = Temp
End If
Next i
End Function
Function ExtractArray(InArray() As Variant, First As Long, Length As Long) As Variant()
On Error Resume Next
Dim i As Long, Arr() As Variant
ReDim Arr(Length - 1)
For i = LBound(Arr) To UBound(Arr)
Arr(i) = InArray(First + i)
Next i
ExtractArray = Arr
End Function
* EDIT - 添加镜像分配 *
Sub GenerateHeatData()
Dim i As Long, j As Long, Left() As Variant, Right() As Variant
Dim Contestants As Long: Contestants = 10
Dim Heats As Long: Heats = 6 ' Heats should be even
Dim BaseArray() As Variant: ReDim BaseArray(Contestants - 1)
For i = 0 To UBound(BaseArray)
BaseArray(i) = i + 1
Next i
For i = 0 To Heats / 2 - 1
Call RandomiseArray(BaseArray)
Left = ExtractArray(BaseArray, 0, WorksheetFunction.Ceiling(UBound(BaseArray) / 2, 1))
Right = ExtractArray(BaseArray, UBound(Left) + 1, UBound(BaseArray) - UBound(Left))
Call RandomiseArray(Left)
For j = 0 To UBound(Left)
ThisWorkbook.ActiveSheet.Cells(j + 1, 3 * i + 1) = CLng(Left(j))
Next j
Call RandomiseArray(Left)
For j = 0 To UBound(Left)
ThisWorkbook.ActiveSheet.Cells(j + 1, 3 * (i + Heats / 2) + 2) = CLng(Left(j))
Next j
Call RandomiseArray(Right)
For j = 0 To UBound(Right)
ThisWorkbook.ActiveSheet.Cells(j + 1, 3 * i + 2) = CLng(Right(j))
Next j
Call RandomiseArray(Right)
For j = 0 To UBound(Right)
ThisWorkbook.ActiveSheet.Cells(j + 1, 3 * (i + Heats / 2) + 1) = CLng(Right(j))
Next j
Next i
End Sub
Function RandomiseArray(Arr() As Variant)
Dim i As Long, j As Long
Dim Temp As Variant
Randomize
For i = LBound(Arr) To UBound(Arr)
j = CLng(((UBound(Arr) - i) * Rnd) + i)
If i <> j Then
Temp = Arr(i)
Arr(i) = Arr(j)
Arr(j) = Temp
End If
Next i
End Function
Function ExtractArray(InArray() As Variant, First As Long, Length As Long) As Variant()
On Error Resume Next
Dim i As Long, Arr() As Variant
ReDim Arr(Length - 1)
For i = LBound(Arr) To UBound(Arr)
Arr(i) = InArray(First + i)
Next i
ExtractArray = Arr
End Function