随机排序变量

时间:2014-04-02 17:25:21

标签: vb.net

我在变量a,b,c和d中有4个字符串。我需要以这样的方式随机排序这些变量,以便我可以将它们输入到4个不同的文本框中,但每次运行程序时都不能输入相同的文本框。

我试图通过将字符串放入数组来为自己简化它。告诉我我做错了什么,或者我有什么方法可以做得更容易。

    Private Sub Random()
    For i = 1 To 4
        If a = 0 Then
            a = r.Next(2, 5)
        ElseIf b = 0 Then
            Do Until b <> a
                b = r.Next(2, 5)
            Loop
        ElseIf c = 0 Then
            Do Until c <> a Or c <> b
                c = r.Next(2, 5)
            Loop
        ElseIf d = 0 Then
            Do Until d <> a Or d <> b Or d <> c
                d = r.Next(2, 5)
            Loop
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

这是一种方法:

Dim a As String = "a"
Dim b As String = "b"
Dim c As String = "c"
Dim d As String = "d"

Dim all As String() = {a, b, c, d}
Dim random As New Random

Dim allRandom As String() = all.OrderBy(Function() random.Next).ToArray