Vb.net 用于随机数生成器的Visual Basic文本框数组

Vb.net 用于随机数生成器的Visual Basic文本框数组,vb.net,random,Vb.net,Random,我想做一个随机数发生器。是学校作业用的。我就是不能让它工作 这项任务是制作一个随机数生成器,将20个数字生成到20个文本框中,以询问10个数学问题 它需要尽可能高效。我需要帮助,因为我只能这样做,但它的效率非常低,而且代码非常长 Dim RandomNumberAHigh As Integer = 12 'Here I am declaring my highest number for the random number generator to use. This will be the h

我想做一个随机数发生器。是学校作业用的。我就是不能让它工作

这项任务是制作一个随机数生成器,将20个数字生成到20个文本框中,以询问10个数学问题

它需要尽可能高效。我需要帮助,因为我只能这样做,但它的效率非常低,而且代码非常长

Dim RandomNumberAHigh As Integer = 12 'Here I am declaring my highest number for the random number generator to use. This will be the highest number the maths quiz will ask the students in the questions.
Dim RandomNumberALow As Integer = 1 'Here I am declaring my lowest number for the random number generator to use. This will be the lowest number the maths quiz will ask the students in the questions.
Dim Random As Integer = 0 'Her I am declaring a variable that will be used to insert the numbers from the random number generator in the quiz text boxes.
Randomize()
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd1.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd4.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd7.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd10.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd13.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd16.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd19.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd22.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd25.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd28.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd3.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd6.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd9.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd12.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd15.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd18.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd21.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd24.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd27.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd30.Text = (Random)

尝试对所有文本框使用foreach循环,如下所示:

Dim RandomNumberAHigh As Integer = 12
Dim RandomNumberALow As Integer = 1
Dim Random As Integer = 0
For Each txt As Control In Me.Controls
    If TypeOf txt Is TextBox Then
        txt.Text = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
    End If
Next

我可能会循环浏览页面上的所有控件,检查它是否是文本框,然后为它指定一个数字(如果是)。此外,使用一个函数来生成你的号码,以清理和加速它

对于速度问题,可能是因为您每次都在创建一个新的随机实例。请看下面的函数

  'Create a control object, loop through every control. If its a textbox, generate your number         
  Dim cControl As Control
    For Each cControl InMe.Controls
        If (TypeOf cControl Is textbox) Then
            cControl.Text = GetRandom(RandomNumberALow,RandomNumberAHigh)
        End If
    Next cControl

 Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
 ' by making Generator static, we preserve the same instance '
 ' (i.e., do not create new instances with the same seed over and over) '
 ' between calls '
     Static Generator As System.Random = New System.Random()
     Return Generator.Next(Min, Max)
 End Function
就像@Idle_Mind的答案(在你的重复线程中..可能会被删除),它使用Textbox的Name属性来确定是否应该为Text属性分配一个生成的值

这里的区别是我通过控件集合枚举;在For Each循环中使用IEnumerable。通过这种方式,我不必根据控件集合的长度检查控件集合,也不必在循环的处理过程中强制转换控件

Sub RandomizeTextBoxes(min As Integer, max As Integer)
    Dim r As Random = New Random()
    Dim RandomTextboxes As IEnumerable(Of TextBox) = Me.Controls.OfType(Of TextBox).Where(AddressOf DoesTextboxNameContainRnd)
    For Each tbControl As TextBox In RandomTextboxes
        tbControl.Text = r.Next(min, max)
    Next
End Sub

Function DoesTextboxNameContainRnd(tbControl As TextBox) As Boolean
    Return tbControl.Name.Contains("Rnd")
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RandomizeTextBoxes(1, 50)
End Sub


通过将addressof
DoesTextboxNameContainRnd
函数传递给集合(借助where方法),我将传递一个predictate函数,该函数将针对集合中的每个对象进行测试,并在该谓词中返回true的对象的结果新集合。这种编程风格被称为反向编程。

此外,可以使用这种风格修改文本框集合的textbox.text值,从而根本不执行for循环。如果不先执行
Randomize(),则不使用
Rnd()