Saya menggunakan metode yang mirip dengan apa yang diposting oleh Gary's Student, tetapi saya menggunakan RANK
formula saya sebagai gantinya. Saya pikir ini menyederhanakan formula dan membuatnya sedikit lebih mudah dimengerti.
Untuk sampel data dalam A1:G1
:
dog mouse rhino ape cat fish rat
Isi formula =RAND()
di seberang A2:G2
.
Kemudian isi formula di bawah ini A3:G3
.
=INDEX($A$1:$G$1,RANK(A2,$A2:$G2))
Ini bagus untuk satu kali atau sejumlah kecil baris.
Untuk solusi yang lebih kuat, saya akan menggunakan VBA. Makro di bawah ini akan memungkinkan Anda untuk memilih nilai yang ingin Anda kocok dan tentukan jumlah permutasi yang ingin Anda buat. Permutasi akan dicetak ke lembar baru, tempat Anda dapat menyalin dan menempelkannya di mana pun Anda suka.
Sub nPerm()
Dim ValuesToPermute As Range, arrIn() As Variant, arrTmp() As Variant
Dim pcount As Long
Dim arrOut() As Variant, shtOut As Worksheet
'Get values to permute from user input
Set ValuesToPermute = Application.InputBox("Select values to permute. (Input must be in a single row.)", Type:=8)
'Get number of permutations wanted from user input
pcount = Application.InputBox("How many permutations would you like?", Type:=1)
'Set up array to hold input
arrIn = ValuesToPermute.Value
'Set up array to hold output
ReDim arrOut(1 To pcount, 1 To UBound(arrIn, 2)) As Variant
'Populate output array with n randomly permuted sets
For i = 1 To pcount
arrTmp = ShuffleArray(arrIn)
For k = 1 To UBound(arrTmp, 2)
arrOut(i, k) = arrTmp(1, k)
Next k
Next i
'Create new sheet and print output there
Set shtOut = Worksheets.Add
shtOut.Name = "nPerm Output"
shtOut.Range("a1").Resize(UBound(arrOut, 1), UBound(arrOut, 2)).Value = arrOut
End Sub
'Modified code from Chip Pearson
'Source: www.cpearson.com/excel/ShuffleArray.aspx Copyright 2018, Charles H. Pearson
Function ShuffleArray(InArray() As Variant) As Variant()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArray
' This function returns the values of InArray in random order. The original
' InArray is not modified.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim N As Long
Dim Temp As Variant
Dim J As Long
Dim Arr() As Variant
Randomize
L = UBound(InArray, 2) - LBound(InArray, 2) + 1
ReDim Arr(1 To 1, LBound(InArray, 2) To UBound(InArray, 2))
For N = LBound(InArray, 2) To UBound(InArray, 2)
Arr(1, N) = InArray(1, N)
Next N
For N = LBound(InArray, 2) To UBound(InArray, 2)
J = CLng(((UBound(InArray, 2) - N) * Rnd) + N)
Temp = Arr(1, N)
Arr(1, N) = Arr(1, J)
Arr(1, J) = Temp
Next N
ShuffleArray = Arr
End Function
Fungsi ShuffleArray bukan pekerjaan saya.
Sumber: www.cpearson.com/excel/ShuffleArray.aspx Hak Cipta 2018, Charles H. Pearson