Results 1 to 5 of 5

Thread: Random number/letters

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    6

    Resolved Random number/letters

    Here's the code

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim T() As String = {"A", "A", "B", "C", "D", "E", "E", "F", "G", "H", "I", "I", "J", "J", "K", "L", "M", "N", "O", "O", "P", "Q", "R", "S", "T", "U", "U", "V", "W", "X", "Y", "Z"}
    Dim R As Random
    Dim nummer As Integer
    For i = 1 To 9
    R = New Random
    nummer = R.Next(0, 32)
    Select Case i
    Case 1
    LblLetter1.Text = T(nummer)
    Case 2
    LblLetter2.Text = T(nummer)
    Case 3
    LblLetter3.Text = T(nummer)
    Case 4
    LblLetter4.Text = T(nummer)
    Case 5
    LblLetter5.Text = T(nummer)
    Case 6
    LblLetter6.Text = T(nummer)
    Case 7
    LblLetter7.Text = T(nummer)
    Case 8
    LblLetter8.Text = T(nummer)
    Case 9
    LblLetter9.Text = T(nummer)
    End Select
    Next
    End Sub

    When I start my form 9 or atleast 7 of the letters are the same. But when I execute everything step by step then all of the letters are random. Why is that?


    ------> When I do It step by step.
    https://gyazo.com/5c50231ac9d46317057750ae6d89122e
    ------> When I just execute the program normally
    https://gyazo.com/c6cd2cf7827db0af40b3009299d68d88
    Last edited by I'm the doctor.; May 5th, 2017 at 02:57 PM. Reason: Resolved!

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Random number/letters

    Your re-initializing the random number generator in the loop, be better to just declare it at the top of the class, I tried like this and only get a few of same chars...

    Code:
    Private R As New Random
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim T() As String = {"A", "A", "B", "C", "D", "E", "E", "F", "G", "H", "I", "I", "J", "J", "K", "L", "M", "N", "O", "O", "P", "Q", "R", "S", "T", "U", "U", "V", "W", "X", "Y", "Z"}
        Dim lbls() = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9}
        Dim nummer As Integer
        For i = 0 To lbls.Length - 1
            nummer = R.Next(0, T.Length)
            lbls(i).Text = T(nummer)
        Next
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    6

    Resolved Re: Random number/letters

    Quote Originally Posted by Edgemeal View Post
    Your re-initializing the random number generator in the loop, be better to just declare it at the top of the class, I tried like this and only get a few of same chars...

    Code:
    Private R As New Random
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim T() As String = {"A", "A", "B", "C", "D", "E", "E", "F", "G", "H", "I", "I", "J", "J", "K", "L", "M", "N", "O", "O", "P", "Q", "R", "S", "T", "U", "U", "V", "W", "X", "Y", "Z"}
        Dim lbls() = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9}
        Dim nummer As Integer
        For i = 0 To lbls.Length - 1
            nummer = R.Next(0, T.Length)
            lbls(i).Text = T(nummer)
        Next
    End Sub
    Works like a charm!
    Thank you!

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Random number/letters

    Quote Originally Posted by Edgemeal View Post
    Your re-initializing the random number generator in the loop, be better to just declare it at the top of the class,
    This don't explain why run step by step generates random label text!!

    After some test i found it is timing problem, when run normal (without break point) R = New Random is execute very fast, so R.Next(0, 32) always start with the same random seed.

    To confirm i added Threading.Thread.Sleep(250) at the beginning of the loop then run normally, issue is gone.



  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Random number/letters

    Right. The default seed for the random number generator is the system time down to the second. If the loop occurred more than once a second, you would be guaranteed to get the same sequence multiple times. For this reason, it is important to only have one random number generator created per second. The easiest way to guarantee that is to only have one random number generator...period.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width