Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to create with this code, to do a multiple random (ie a loop) to do a random 20-30 times. how could i do that? and I want to increase the value of the +1 textbox each time it goes to the next random step.

TextBox1.Text = Val(TextBox1.Text) + 1

Code:

   Dim strWords As String() = str1.Split(",")
        'Create an instance of the Random class
        Dim ValRnd As Integer = TxtNumRnd.Text
        Dim rnd As New Random(ValRnd)' (Values Random not worked)
        'Get a random number from 1 to 80  (2 digits)
        TextBox1.Text = Val(TextBox1.Text) + 1
        Dim randomNumber As Integer = rnd.Next(0, 81)
        If randomNumber = strWords(StrwrVal.Text) Then
            Exit For
        Else
            TxtRnd1.Text = TxtRnd1.Text & vbNewLine & randomNumber
        End If
    Next
Next

Values random not worked.

Dim rnd As New Random(5)'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
131 views
Welcome To Ask or Share your Answers For Others

1 Answer

While this may not be a direct answer it should serve to clarify a few things.

The Random Constructor - as I stated in the comments, using New Random(5) does not generate a sequence of 5 random numbers for you. It simply sets the seed for your random number generator. This means that the sequence of numbers that you generate when calling Random.Next() will follow the same pattern as they all have the same seed (see example).

Note: Ideally, when creating a New Random() do not set a seed value. They current time will be used as the default seed.


Creating a New Random(x) inside a loop with a defined seed

'Since random is declared inside the loop, using the same seed value 
'each time the loop executes, the same random sequence would be generated.
'Random.Next() will then continually access the first value in the sequence. 
For i = 1 To 5
    Dim rnd As New Random(5)
    TextBox1.AppendText($"{rnd.Next(0, 11)} | ")
Next

Output: 3 | 3 | 3 | 3 | 3


Creating a New Random(x) outside of the loop with a defined seed

'Since random is now declared outside the loop, and Random.Next() is called
'inside the loop, the output sequence actually progresses.
'Note that the first number is the same as the previous example as the seed is the same. 
Dim rnd As New Random(5)
    For i = 0 To 5
    TextBox1.AppendText($"{rnd.Next(0, 11)} | ")
Next

Output: 3 | 3 | 2 | 6 | 5 | 10


Creating multiple instances of New Random(x) all with the SAME seed

        'Instance 1
        Dim rnd1 As New Random(5)
        For i = 0 To 5
            TextBox1.AppendText($"{rnd1.Next(0, 11)} | ")
        Next

        'Instance 2
        Dim rnd2 As New Random(5)
        For i = 0 To 5
            TextBox2.AppendText($"{rnd2.Next(0, 11)} | ")
        Next

        Instance 3
        Dim rnd3 As New Random(5)
        For i = 0 To 5
            TextBox3.AppendText($"{rnd3.Next(0, 11)} | ")
        Next

Instance 1 Output: 3 | 3 | 2 | 6 | 5 | 10
Instance 2 Output: 3 | 3 | 2 | 6 | 5 | 10
Instance 3 Output: 3 | 3 | 2 | 6 | 5 | 10


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...