Question Having some random number problems

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
165
Location
Chicago
Programming Experience
10+
I'm working on a random email generator for a client, and for some unknown reason, it's not very random at all. It very very frequently returns the same item. Can anyone point out the issue? I'll paste the entire module.

All that is needed once the module is created is to call the function"GenerateRandomEM", i.e. Debug.Print(GenerateRandomEM)


VB.NET:
Module modRandomEmailGenerator

    Public Function RandomName() As String
        ' This function returns a randomly generated word

        RandomName = String.Empty

        Dim aRandom As New ArrayList(18)

        ' Add a bunch of words to the array
        With aRandom
            .Add("sap")
            .Add("sat")
            .Add("sad")
            .Add("rat")
            .Add("rap")
            .Add("ram")
            .Add("rag")
            .Add("nap")
            .Add("Nat")
            .Add("mat")
            .Add("map")
            .Add("mad")
            .Add("lap")
            .Add("lag")
            .Add("lad")
            .Add("fat")
            .Add("fan")
            .Add("fad")
        End With

        Return aRandom.Item(GenerateRandomNumber(0, 18)).ToString()

    End Function

    Public Function RandomCharacter() As String

        RandomCharacter = String.Empty

        ' Returns a randomly generated character
        Dim aCharacter As New ArrayList(2)

        With aCharacter
            .Add("-")
            .Add("_")
        End With

        Return aCharacter.Item(GenerateRandomNumber(0, 2)).ToString()

    End Function

    Public Function GenerateRandomNumber(ByVal intMin As Integer, ByVal intMax As Integer) As Integer

        GenerateRandomNumber = 0

        ' Initialize random number generator
        Dim r As New Random(System.DateTime.Now.Millisecond)

        Return r.Next(intMin, intMax)

    End Function

    Public Function GenerateRandomEM() As String
        ' Generates a random email to use

        GenerateRandomEM = String.Empty

        Dim strFirstName As String = RandomName()
        Dim strLastName As String = RandomName()

        'If strLastName = strFirstName Then
        '    ' Confirm that we have 2 different names
        '    strLastName = RandomName()
        'End If

        Dim intEmailStyle As Integer = GenerateRandomNumber(0, 6)

        Select Case intEmailStyle

            Case 0
                ' Ex: Returns John-Doe or John_Doe
                GenerateRandomEM = strFirstName & RandomCharacter() & strLastName
            Case 1
                ' Ex: Returns John-Doe1234 or John_Doe1234
                GenerateRandomEM = strFirstName & RandomCharacter() & strLastName & GenerateRandomNumber(0, 9999).ToString
            Case 2
                ' Ex: Returns John-Doe_1234 or John_Doe-1234 or similar
                GenerateRandomEM = strFirstName & RandomCharacter() & strLastName & RandomCharacter() & GenerateRandomNumber(0, 9999).ToString
            Case 3
                ' Ex: Returns John1234Doe
                GenerateRandomEM = strFirstName & GenerateRandomNumber(0, 9999).ToString & strLastName
            Case 4
                ' Ex: Returns John-1234Doe or John_1234Doe 
                GenerateRandomEM = strFirstName & RandomCharacter() & GenerateRandomNumber(0, 9999).ToString & strLastName
            Case 5
                ' Ex: Returns John1234-Doe or John1234_Doe 
                GenerateRandomEM = strFirstName & GenerateRandomNumber(0, 9999).ToString & RandomCharacter() & strLastName
            Case 6
                ' Ex: Returns John-1234-Doe or John_1234_Doe or similar
                GenerateRandomEM = strFirstName & RandomCharacter() & GenerateRandomNumber(0, 9999).ToString & RandomCharacter() & strLastName
        End Select

        GenerateRandomEM = GenerateRandomEM & "@email.com"

    End Function

End Module
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
Create only one instance of the Random class and use that to generate all random numbers.

Instead of ArrayList with String items use List(Of String).
 

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
165
Location
Chicago
Programming Experience
10+
Thanks again John, that worked.

For educational purposes I ask, how come using a new instance each time would give the same random number?

Also, why switch from arraylist to list? Less overhead? Faster?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
how come using a new instance each time would give the same random number?
I believe documentation describes this well: Random Class (System)
Also, why switch from arraylist to list? Less overhead? Faster?
Generics gets you strongly typed objects. Since you ask I recommend you turn on Option Strict.
 

Latest posts

Top Bottom