0

乱数発生器のコーディングに何を追加すれば、数字が連続して繰り返されなくなりますか?

私の乱数ジェネレーターは次のようになります。

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
4

4 に答える 4

1

与えられた N (現在は N = 3 ですが、あなたが言うように、それは何か他のものである可能性があります)、1、...、N のランダムな順列を作成してから、生成された順序でテキスト ボックスを開きます。これは、一度に N 個の数値を生成し、それらをすべて使い切ってから、さらに N 個生成することを意味することに注意してください。「ランダム順列」を検索して、アルゴリズムを見つけます。

于 2013-10-29T22:13:53.553 に答える
1

Random インスタンス "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
于 2013-10-29T22:18:43.053 に答える
0

1 から N (両端を含む) の間のランダムな整数値を取得するには、次を使用できます。

CInt(Math.Ceiling(Rnd() * n))
于 2013-10-29T21:47:42.033 に答える
0

各番号を 1 回だけ使用する場合は、次のようにする必要があります。

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
于 2013-10-29T22:23:13.023 に答える