-1

hi please check this code

For icnt As Integer = 1 To 100
        Me.SetText(icnt)
        Dim gd As String = ""
        Dim gl As String = ""
        Dim cr As String = ""
        Dim lr As String = ""
        Dim tbs As String = ""
        Dim srt As String = ""
        Dim fp As String = ""
        Dim ky As String = ""
        Dim prx As String = ""

        Dim rnd As New Random
        Dim scrpurl As String = ""
        Dim bldstr As New List(Of String)

        scrpurl = GlobalVariables.domain(rnd.Next(0, GlobalVariables.googledomain.Count - 1)) & "/search?q="
        scrpurl = scrpurl & GlobalVariables.prints(rnd.Next(0, GlobalVariables.footprints.Count - 1))
        scrpurl = scrpurl & GlobalVariables.words(rnd.Next(0, GlobalVariables.keywords.Count - 1))
        scrpurl = scrpurl & GlobalVariables.gl(rnd.Next(0, GlobalVariables.googlegl.Count - 1))
        scrpurl = scrpurl & GlobalVariables.cr(rnd.Next(0, GlobalVariables.googlecr.Count - 1))
        scrpurl = scrpurl & GlobalVariables.lr(rnd.Next(0, GlobalVariables.googlelr.Count - 1))
        scrpurl = scrpurl & GlobalVariables.tbs(rnd.Next(0, GlobalVariables.googletbs.Count - 1))
        scrpurl = scrpurl & GlobalVariables.start(rnd.Next(0, GlobalVariables.googlestart.Count - 1))

        MsgBox(scrpurl)

        bldstr.Clear()
        bldstr.Add(scrpurl)

        Dim dr3 As DataRow() = dtse.Select("seurl ='" & scrpurl & "'", "se")
        If (dr3.Count = 0) Then
            Dim w1 As WaitCallback = New WaitCallback(AddressOf setdata)
            ThreadPool.QueueUserWorkItem(w1, bldstr)
        End If

    Next

Can anyone tell me when i show the messagebox i see different values for scrpurl and setdata adds correct and 100 unique rows.

When i dont show it setdata adds 100 rows of same scrpurl. I know something should be done so that each setdata gets different scrpurl but iam missing something here. Can anyone please help me out?

Regards,

xhammer
  • 145
  • 2
  • 12

1 Answers1

1

You are creating a new Random object each time you iterate through your loop. If you move the Dim rnd As New Random line up to before the for-loop, it will work fine.

The reason for this is because computer-generated random numbers are not really random--they merely appear to be random. The way it works is by first determining a "seed" value. Then, it uses a mathematical formula whereby inputing the seed value plus an additional sequence value, the next (seemingly) random number can be gotten. Therefore, whenever the seed value is the same, the sequence of random numbers that it generates will always be the same. The way the Random class generates it's seed value is by basing it off of the current time when the object is created. Therefore, if you create a bunch of Random objects at the same time, they will all generate the exact same sequence of random numbers.

Because of this, it's typically best-practice to create one Random object when the application starts and then re-use the same one everywhere rather than creating a new one every time you need one.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • lol ok thanks for answer. it worked. I thought random is an actual random method and yeah i will focus on only one random from now on. – xhammer Jan 09 '13 at 12:42