Visual Basic Text Box Array for random number gene

2019-06-14 20:37发布

问题:

I'm wanting to make a random number generator. It's for school homework. I just can't seem to get it to work.

The task was to make a random number generator to generate 20 numbers into 20 text boxes to ask 10 maths questions.

It needs to be as efficient as possible. I need help as I can only do it but it's really inefficient and really long code.

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)

回答1:

Try to use a foreach loop for all textboxes as below:

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


回答2:

I would probably loop through all the controls on the page, checking if it is a text box and then assign it a number if it is. Additionally, use a function to generate your number to clean and speed it up.

For your speed issue, it is likely because you are creating a new random instance everytime. Look at the function below.

  '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


回答3:

Like @Idle_Mind's answer (in your duplicate thread.. which will probably be erased), this uses the Textbox's Name property to determine whether it should have the Text Property assigned a generated value.

The difference here is I am enumerating through the control collection; using IEnumerable in a For Each loop. In this manner, I don't have to check through the control collection, against the length of the control collection, or cast the control within the processing of the loop.

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

By passing the addressof DoesTextboxNameContainRnd function to the collection (by virtue of the where method), I'm passing a predictate function that will be tested against each object in the collection, and the resulting new collection of the objects that returned true in that predicate. This style of programming is referred to as contra-varient programming.



标签: vb.net random