随机属性麻烦VB

时间:2013-06-11 12:44:17

标签: vb.net function random

  Randomize()
        Dim value As Integer = CInt(Int((10 * Rnd()) + 1))
        num1 = value
        num2 = value

        If TextBox2.Text = (num1 * num2) Then
            TextBox3.Text = " correct ! "
        Else
            TextBox3.Text = "sorry, try again"

        End If

我无法让Num1 / num2等于1-10。

我如何编码以使num1和num2等于数字1-10?

3 个答案:

答案 0 :(得分:1)

这说明了如何使用.Net的Random类。用它代替Rnd。

Dim prng As New Random

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim value As Integer = prng.Next(1, 11) '1-10 upper range is exclusive
    Dim num1 As Integer = prng.Next(1, 11) '1-10 upper range is exclusive
    Dim num2 As Integer = prng.Next(1, 11) '1-10 upper range is exclusive
End Sub

答案 1 :(得分:1)

Randomize()
        Dim value As Integer = CInt(Int((10 * Rnd()) + 1))
        Dim num1 As Integer = value
        value = CInt(Int((10 * Rnd()) + 1))
        Dim num2 As Integer = value

这将起作用

答案 2 :(得分:0)

你有num1 = valuenum2 = value

价值只是我们正在创造的变量。

randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))

所以对于你的代码:

 Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))

 Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))

此示例使用Rnd函数生成1到6范围内的随机整数值。 VB

' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 6.
Dim value As Integer = CInt(Int((6 * Rnd()) + 1))

http://msdn.microsoft.com/en-us/library/f7s023d2(v=VS.80).aspx