我正在vb.net中开发一个网页,它将为用户生成许多选择题。我需要将已经放入数组的四个答案洗牌。让我们假设我必须遵循数组:
array = {"Correct", "Wrong1", "Wrong2", "Wrong3"}
我尝试使用以下方法:
Public Shared Function Shuffle(ByVal items() As String) As Array
Dim max_index As Integer = items.Length - 1
Dim rnd As New Random(DateTime.Now.Millisecond)
For i As Integer = 0 To max_index
' Pick an item for position i.
Randomize()
Dim j As Integer = rnd.Next(i, max_index)
' Swap them.
Dim temp As String = items(i)
items(i) = items(j)
items(j) = temp
Next i
Return items
End Function
这个功能工作得非常好,但我的问题是,如果我有四个问题,每个问题的答案都会被洗牌,但正确答案会在一个位置如下:
array = {"Wrong1", "Correct", "Wrong2", "Wrong3"}
array = {"Wrong2", "Correct", "Wrong3", "Wrong1"}
array = {"Wrong3", "Correct", "Wrong1", "Wrong2"}
array = {"Wrong1", "Correct", "Wrong3", "Wrong2"}
我需要的是将其位置从一个问题更改为另一个问题的正确答案。 感谢您的帮助。
答案 0 :(得分:6)
您的Shuffle方法和Random
的使用存在一些问题。
Randomize
用于旧的传统VB Rnd
函数。它对闪亮的新NET Random
类没有影响。为整个应用创建一个Random
,而不是每次随机播放(或点击某些内容)。永远不要在循环中创建它们 - 这几乎可以保证重复的数字。
Random.Next(min, max)
的最大参数是独占,因此您的范围实际上比它应该小1个元素。
Sub
),使其非常有效(而不是返回一个新的集合)标准Fisher-Yates Shuffle:
' form/class level var
Private rnd As New Random()
Public Sub Shuffle(items As String())
Dim j As Int32
Dim temp As String
For n As Int32 = items.Length - 1 To 0 Step -1
j = rnd.Next(0, n + 1)
' Swap them.
temp = items(n)
items(n) = items(j)
items(j) = temp
Next n
End Sub
从同一起始阵列的4次shuffle输出:
Shuffle #1 Wrong3 Correct Wrong2 Wrong1
Shuffle #2 Correct Wrong2 Wrong1 Wrong3
Shuffle #3 Wrong2 Correct Wrong3 Wrong1
Shuffle #4 Correct Wrong1 Wrong2 Wrong3
Shuffle #5 Correct Wrong1 Wrong3 Wrong2
您可以传递它(可用作扩展方法),而不是全局Random
生成器:
Public Sub Shuffle(items As String(), RNG As Random)
改组许多类型的通用方法:
' Generic shuffle for basic type arrays
Public Sub Shuffle(Of T)(items As T(), rng As Random)
Dim temp As T
Dim j As Int32
For i As Int32 = items.Count - 1 To 0 Step -1
' Pick an item for position i.
j = rng.Next(i + 1)
' Swap
temp = items(i)
items(i) = items(j)
items(j) = temp
Next i
End Sub
示例:
Shuffle(intArray, myRnd)
Shuffle(strArray, myRnd)
Shuffle(colors, myRnd)
Shuffle(myButtons, myRnd)
简单重新排序
最后,对于一些只能重新排序它们的东西,使用扩展方法的简单且通常足够好的版本:
Dim ShuffledItems = myItems.OrderBy(Function() rnd.Next).ToArray()
代码少,易于删除,但 效率低下。
1 您想要向后循环的原因是将每个数组元素限制为 一个 交换。一次" X"在最后一步中移动到items(j)
,因此rnd.Next(0, n + 1)
只会选择最后最后一个元素/循环索引,因此该位置将被删除。许多实现都错了。