Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27056

Random.Next generating same value repeatedly?

$
0
0
Code:

Public Class TestObject
    Public Property RandomNumber As Integer

    Private rand As Random

    Public Sub New()
        rand = New Random
        Me.RandomNumber = rand.Next(0, 10000)
    End Sub
End Class

When I create many of these test objects too add too a list, they are all generating the same random number. I used to have the rand variable within the New method, but made it a field instead, but that didn't solve my problem...

P.S. I know this seems like a small, insignificant issue, but my program actually won't be able to work unless I figure this out..

EDIT: Do I seriously have to pause thread execution for 1 millisecond to make sure the initial seed value is different each time is a test object is created? And in a background thread so that is doesn't pause the UI thread? That's ridiculous just to generate a list of random numbers...

EDIT 2: I just tested pausing execution for 10 milliseconds between creating each object in a loop. Some numbers are still repeating, but it isn't nearly as bad as without the pausing. This is unacceptable though because it takes to long to create a large list with the pause. Also I can't have repeating numbers.

Viewing all articles
Browse latest Browse all 27056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>