VB .Net Shuffle 二维字符串数组 - 更优雅
VB .Net Shuffle 2 dimensional array of Strings - more elegantly
有时你只是知道你会因为一个问题而被拒绝,但这里什么都没有。
我有一个名为问题的二维字符串数组,是的,它是测验。在不将原始 "Questions" 数组类型更改为更简单的结构列表的情况下,是否有更优雅的方式来打乱问题的顺序?
这是我的:
'1st I create 3 arrays to hold the 3 components of each Question
Dim arrQ((Questions.Length / 4) - 1) As String 'question
Dim arrA((Questions.Length / 4) - 1) As String 'answer
Dim arrM((Questions.Length / 4) - 1) As String 'media name
'2nd I copy the values from question array into individual arrays
Dim z As Integer
For z = 0 To (Questions.Length / 4) - 1
arrQ(z) = Questions(0, z)
arrA(z) = Questions(1, z)
arrM(z) = Questions(2, z)
Next
'create an array to hold our shuffled questions
Dim x As Integer
Dim randarray(total_quizquestions - 1) As Integer
'create a list that we can remove index once they've been added to randarray
Dim list As New ArrayList
For i As Integer = 0 To total_quizquestions - 1
list.Add(i)
Next i
'add and remove
Dim rand As New Random
Dim index As Integer
For x = 0 To total_quizquestions - 1
index = rand.Next(0, list.Count)
randarray(x) = list(index)
list.RemoveAt(index)
Next
'clear original Questions
ReDim Preserve Questions(3, total_quizquestions - 1)
'add back to questions using randarray random number to get rows from arrQ etc.
Dim f As Integer = 0
For f = 0 To total_quizquestions - 1
Questions(0, f) = arrQ(randarray(f))
Questions(1, f) = arrA(randarray(f))
Questions(2, f) = arrM(randarray(f))
Next f
嗨,转眼间,我的代码可以工作,但是太丑了,我很惭愧!哦,是的,问题确实有 4 个元素,但我只对前 3 个感兴趣。
善待...
LINQ 不能很好地处理 2D 数组,因此您不更改基本数组结构的要求排除了许多漂亮、优雅的解决方案。
也就是说,您可以使用 就地随机化数组。
此代码基于我在上一段中链接的答案(归功于 Nat Pongjardenlarp)。我已将其适应您的二维阵列。由于您没有提供 MCVE,因此完全未经测试。
Dim rnd As New Random()
For n = total_quizquestions - 1 To 0 Step -1
Dim j = rnd.Next(0, n + 1)
' Swap all three components of the question
For component = 0 To 2
Dim temp = Questions(component, n)
Questions(component, n) = Questions(component, j)
Questions(component, j) = temp
Next component
Next n
而且,只是为了好玩(和后代),这是一个没有 "magic numbers" 的通用(和测试)版本,它随机播放 any 二维数组:
Private rnd As New Random()
Sub Shuffle2DArray(Of T)(arr As T(,))
For row = arr.GetUpperBound(1) To arr.GetLowerBound(1) Step -1
Dim swapRow = rnd.Next(0, row + 1)
' Swap all columns of the row
For column = arr.GetLowerBound(0) To arr.GetUpperBound(0)
Dim temp = arr(column, row)
arr(column, row) = arr(column, swapRow)
arr(column, swapRow) = temp
Next column
Next row
End Sub
显然,您可以交换 Get...Bound(0)
和 Get...Bound(1)
以沿二维数组的另一个轴随机播放。
有时你只是知道你会因为一个问题而被拒绝,但这里什么都没有。
我有一个名为问题的二维字符串数组,是的,它是测验。在不将原始 "Questions" 数组类型更改为更简单的结构列表的情况下,是否有更优雅的方式来打乱问题的顺序?
这是我的:
'1st I create 3 arrays to hold the 3 components of each Question
Dim arrQ((Questions.Length / 4) - 1) As String 'question
Dim arrA((Questions.Length / 4) - 1) As String 'answer
Dim arrM((Questions.Length / 4) - 1) As String 'media name
'2nd I copy the values from question array into individual arrays
Dim z As Integer
For z = 0 To (Questions.Length / 4) - 1
arrQ(z) = Questions(0, z)
arrA(z) = Questions(1, z)
arrM(z) = Questions(2, z)
Next
'create an array to hold our shuffled questions
Dim x As Integer
Dim randarray(total_quizquestions - 1) As Integer
'create a list that we can remove index once they've been added to randarray
Dim list As New ArrayList
For i As Integer = 0 To total_quizquestions - 1
list.Add(i)
Next i
'add and remove
Dim rand As New Random
Dim index As Integer
For x = 0 To total_quizquestions - 1
index = rand.Next(0, list.Count)
randarray(x) = list(index)
list.RemoveAt(index)
Next
'clear original Questions
ReDim Preserve Questions(3, total_quizquestions - 1)
'add back to questions using randarray random number to get rows from arrQ etc.
Dim f As Integer = 0
For f = 0 To total_quizquestions - 1
Questions(0, f) = arrQ(randarray(f))
Questions(1, f) = arrA(randarray(f))
Questions(2, f) = arrM(randarray(f))
Next f
嗨,转眼间,我的代码可以工作,但是太丑了,我很惭愧!哦,是的,问题确实有 4 个元素,但我只对前 3 个感兴趣。 善待...
LINQ 不能很好地处理 2D 数组,因此您不更改基本数组结构的要求排除了许多漂亮、优雅的解决方案。
也就是说,您可以使用
此代码基于我在上一段中链接的答案(归功于 Nat Pongjardenlarp)。我已将其适应您的二维阵列。由于您没有提供 MCVE,因此完全未经测试。
Dim rnd As New Random()
For n = total_quizquestions - 1 To 0 Step -1
Dim j = rnd.Next(0, n + 1)
' Swap all three components of the question
For component = 0 To 2
Dim temp = Questions(component, n)
Questions(component, n) = Questions(component, j)
Questions(component, j) = temp
Next component
Next n
而且,只是为了好玩(和后代),这是一个没有 "magic numbers" 的通用(和测试)版本,它随机播放 any 二维数组:
Private rnd As New Random()
Sub Shuffle2DArray(Of T)(arr As T(,))
For row = arr.GetUpperBound(1) To arr.GetLowerBound(1) Step -1
Dim swapRow = rnd.Next(0, row + 1)
' Swap all columns of the row
For column = arr.GetLowerBound(0) To arr.GetUpperBound(0)
Dim temp = arr(column, row)
arr(column, row) = arr(column, swapRow)
arr(column, swapRow) = temp
Next column
Next row
End Sub
显然,您可以交换 Get...Bound(0)
和 Get...Bound(1)
以沿二维数组的另一个轴随机播放。