Vb.net随机数生成器重复相同的数字

Vb.net随机数生成器重复相同的数字,vb.net,random,Vb.net,Random,我应该在随机数生成器编码中添加什么,使数字不会在一行中重复更多次 我的随机数生成器如下所示: Dim rn As New Random TextBox1.Text = rn.Next(1, 4) If TextBox1.Text = 1 Then Form4.Show() Form4.Timer1.Start() End If If TextBox1.Text = 2 Then Form7.Show() Form7.Timer1.Start() End If

我应该在随机数生成器编码中添加什么,使数字不会在一行中重复更多次

我的随机数生成器如下所示:

Dim rn As New Random
TextBox1.Text = rn.Next(1, 4)
If TextBox1.Text = 1 Then
    Form4.Show()
    Form4.Timer1.Start()
End If

If TextBox1.Text = 2 Then
    Form7.Show()
    Form7.Timer1.Start()
End If

If TextBox1.Text = 3 Then
   Form8.Show()
   Form8.Timer1.Start()
End If
Const FirstNumber As Integer = 1
Const LastNumber As Integer = 5

' Fill the list with numbers
Dim numberList as New List(Of Integer)
For i As Integer = FirstNumber To LastNumber Step 1
    numberList.Add(i)
Next i

Dim rand as New Random()
While numberList.Count > 0
    ' draw a random number from the list
    Dim randomIndex As Integer = rand.Next(0, numberList.Count - 1)
    Dim randomNumber As Integer = numberList(randomIndex)

    ' Do stuff with the number here        
    TextBox1.Text = randomNumber

    ' remove the number from the list so it can't be used again
    numberList.RemoveAt(randomIndex)
End While

要获得介于1和N(包括)之间的随机整数值,可以使用以下公式

CInt(Math.Ceiling(Rnd() * n))

给定N(目前N=3,但正如您所说,它可能是其他东西),尝试构造1,…,N的随机排列,然后按生成的顺序打开文本框。请注意,这意味着您一次生成N个数字,并将它们全部用完,然后再生成N个。搜索“随机排列”以查找算法。

将随机实例“rn”移出类(表单)级别,以便只为表单创建一次,并反复使用同一实例:

Public Class Form1

    Private rn As New Random

    Private Sub SomeMethod()
        TextBox1.Text = rn.Next(1, 4)
        If TextBox1.Text = 1 Then
            Form4.Show()
            Form4.Timer1.Start()
        End If

        If TextBox1.Text = 2 Then
            Form7.Show()
            Form7.Timer1.Start()
        End If

        If TextBox1.Text = 3 Then
            Form8.Show()
            Form8.Timer1.Start()
        End If
    End Sub

End Class

如果希望每个数字只使用一次,则需要执行以下操作:

Dim rn As New Random
TextBox1.Text = rn.Next(1, 4)
If TextBox1.Text = 1 Then
    Form4.Show()
    Form4.Timer1.Start()
End If

If TextBox1.Text = 2 Then
    Form7.Show()
    Form7.Timer1.Start()
End If

If TextBox1.Text = 3 Then
   Form8.Show()
   Form8.Timer1.Start()
End If
Const FirstNumber As Integer = 1
Const LastNumber As Integer = 5

' Fill the list with numbers
Dim numberList as New List(Of Integer)
For i As Integer = FirstNumber To LastNumber Step 1
    numberList.Add(i)
Next i

Dim rand as New Random()
While numberList.Count > 0
    ' draw a random number from the list
    Dim randomIndex As Integer = rand.Next(0, numberList.Count - 1)
    Dim randomNumber As Integer = numberList(randomIndex)

    ' Do stuff with the number here        
    TextBox1.Text = randomNumber

    ' remove the number from the list so it can't be used again
    numberList.RemoveAt(randomIndex)
End While

你是说你不想要像
122/311/233
1232/2113/3112
这样的东西吗?是的,现在是1231/233。。。我想要1 3 2/2 1 3/3 1 2…随机数确实重复,尤其是在如此小的范围内。您可以将最后一个数字存储为var,并不断获取新的随机数,直到它与前一个不匹配为止。您可以只按随机顺序排列数字1、2、3。我无法按随机顺序排列它们,因为很快会有更多的问题。我在做测验,所以我需要每次都有所不同。这就是我要做的。为了方便起见,您可以轻松地交换两个随机元素n/2次。这也称为“Fisher-Yates”或“Knuth”洗牌。请看我的答案,他不想重复任何数字,修正了!问题是我有两个新的随机变量,现在它可以正常工作了。感谢您的帮助。从OP中还不清楚这是一副牌还是骰子。