XMLpitstop.com   |  VBnetexpert.com   |  Community Credit  
 
 
Pitstop Search:  
in
 
Sign in | Join | Help
 
 
  Blog
    Home  
 
  Entries By Date
 
<June 2008>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
 
 
  Blog Categories
   
 
  Archives
    December 2009 (2)  
    October 2009 (1)  
    September 2009 (1)  
    August 2009 (2)  
    July 2009 (2)  
    June 2009 (1)  
    May 2009 (1)  
    February 2009 (1)  
    December 2008 (1)  
    November 2008 (3)  
    September 2008 (3)  
    August 2008 (3)  
    June 2008 (4)  
    May 2008 (2)  
    April 2008 (3)  
    March 2008 (3)  
    February 2008 (5)  
    December 2007 (4)  
    November 2007 (1)  
    October 2007 (3)  
 
  Syndication
    RSS  
    Atom  
    Comments RSS  
  .NET Flea Market  
 

A Totally Non-Random Post

I am sure this is covered somewhere on the great big Internet, but it's new to me.  And it sucked until I discovered what the problem was.  Basically, I had random values being generated that were anything but random.  "Oh, you forgot to set a seed value to something pseudo-random like now.millisecond."  No.  That didn't help.  Check out this code and see what the results are:

    Private Sub UnRandom()
        Dim r As New Random(Now.Millisecond)

        Dim winCount, winCount2 As Integer

        For i As Integer = 1 To 100
            If Rndm() > 50 Then winCount += 1
        Next

        For i As Integer = 1 To 100
            If r.Next(1, 100) > 50 Then winCount2 += 1
        Next

        MsgBox(winCount & ":" & winCount2)

    End Sub

    Private Function Rndm() As Integer
        Dim r As New Random(Now.Millisecond)

        Return r.Next(1, 100)
    End Function

Assuming a 50/50 chance of win, you would expect that a Random.Next would return a random value.  Seemingly not when it gets continually instantiated like in the Rndm function.  So this apparently is not a case where you want all of your variables to be contained in their own methods.  You'd probably want to have one single random number generator for your whole application.

Comments

No Comments

About anachostic

That's me. Seek and ye shall find.
 
 
Copyright © . All Rights Reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems