Results 1 to 9 of 9

Thread: Random.Next - Not Very Random At All... (Solved)

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Random.Next - Not Very Random At All... (Solved)

    Hi!
    To cut to the chase, when I want to create four random numbers they are all the same. (I do not want that.) Is this because I have 4 random numbers being generated almost at the same time; do they use the same random seed even though I declare R as NEW random?
    By the way - this is just a test code but my real program does the same thing. Thanks for the help in advance!

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim TestString As String = ""
    4.  
    5.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    6.         Timer1.Stop()
    7.         A()
    8.         B()
    9.         C()
    10.         D()
    11.         Label1.Text = TestString
    12.         TestString = ""
    13.     End Sub
    14.  
    15.     Private Sub A()
    16.         Dim R As New Random
    17.         Dim A As Int32 = CInt(R.Next(0, 100))
    18.         TestString += A.ToString & ", "
    19.     End Sub
    20.  
    21.     Private Sub B()
    22.         Dim R As New Random
    23.         Dim B As Int32 = CInt(R.Next(0, 100))
    24.         TestString += B.ToString & ", "
    25.     End Sub
    26.  
    27.     Private Sub C()
    28.         Dim R As New Random
    29.         Dim C As Int32 = CInt(R.Next(0, 100))
    30.         TestString += C.ToString & ", "
    31.     End Sub
    32.  
    33.     Private Sub D()
    34.         Dim R As New Random
    35.         Dim D As Int32 = CInt(R.Next(0, 100))
    36.         TestString += D.ToString
    37.     End Sub
    38.  
    39.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    40.         Timer1.Start()
    41.     End Sub
    42.  
    43. End Class
    Last edited by NinjaNic; Jan 4th, 2015 at 08:20 PM.

Tags for this Thread

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