如何随机选择0或1 vb.net

时间:2014-01-10 03:11:02

标签: vb.net

我想制作一个能在文本框中显示结果的生成器,我想让它随机选择0或1,我该怎么做?

Kode有效:

Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim r as New Random()
Dim n As Integer = r.Next(2)
TextBox1.Text = CStr(n) 

    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

尝试以下

Dim r as New Random()
Dim n As Integer = r.Next(2)
TextBox1.Text = CStr(n)

答案 1 :(得分:0)

您可以使用.NET Framework中包含的Random class。它有一个名为Next的实例方法,它接受最小/最大值。这是一个例子:

Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim rng As Random = New Random()
    Dim randomNumber As Integer = rng.Next(0,2)
    ' Do what you want with randomNumber from here...
End Sub
End Class