Duplicate random generated numbers

ljpv14

Active member
Joined
Nov 28, 2011
Messages
44
Programming Experience
3-5
Okay I got a situation, I have a code that generates random code using a set of letters. One for a random pin and another for a random code. I can loop the generation of my codes depending on the input. When I try to generate 1 to 3 set of codes it works fine. but if it's 4 and above. I always get a duplicate set of random numbers. I don't what wrong. My code for generating my codes are in vb.net and be passed to a stored procedure for inventory.

Here is my code:

Button event:
Protected Sub genCodeBtn_Click(sender As Object, e As EventArgs) Handles genCodeBtn.Click


        connection = accDB.databaseConnect()
        Dim pin As String = ""
        Dim code As String = ""


        For i As Integer = 1 To CInt(qtyTxtbox.Text)


            pin = generateCode(5, 0)


            code = generateCode(5, 1)


            accDB.accessSp_insert_reg_code(connection, CInt(packCatIdTxtbox.Text), pin, code)


        Next


        MsgBox("Number of affected rows: " + CStr(accDB.getAffectedRows()))


    End Sub

generate code sub:
 Dim keycode As String = ""


        Dim retString As String = ""


        Dim ranvar As Random = New Random()




        If type = 0 Then
            keycode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
        ElseIf type = 1 Then
            keycode = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        End If


        For j As Integer = 0 To len - 1


            retString += keycode(ranvar.Next(keycode.Length - 1))


        Next j


        Return retString


    End Function

databaseConnect method from accessdatabase class:
 Dim connStr As String = ConfigurationManager.ConnectionStrings("Hr_Inventory_Server").ConnectionString


        Dim con As SqlConnection = New SqlConnection(connStr)


        Return con

accessSp_insert_reg_code method in accessdatabase class
 Public Sub accessSp_insert_reg_code(ByVal spConn As SqlConnection, ByVal spPackCatId As Integer, ByVal spPin As String, ByVal spCode As String)
        spConn.Open()


        Dim affectedRows As Integer


        Dim spCmd As SqlCommand = New SqlCommand("sp_insert_reg_code", spConn)


        Dim spParam As SqlParameter = New SqlParameter


        spCmd.CommandType = CommandType.StoredProcedure




        spParam = spCmd.Parameters.Add("@pin", SqlDbType.VarChar)


        spParam.Value = spPin


        spParam = spCmd.Parameters.Add("@code", SqlDbType.VarChar)


        spParam.Value = spCode




        spParam = spCmd.Parameters.Add("@packcatid", SqlDbType.Int)


        spParam.Value = spPackCatId




        affectedRows = spCmd.ExecuteNonQuery()




        setAffectedRows(affectedRows)


        spConn.Close()
    End Sub






I really don't know what causes the duplicates. But I'm pretty sure that my code is right. It's just that at some point when I try to generate 4 or more set of codes. Duplicates begin to arise. Thanks!

PEACE! XD
 
I've added code formatting tags to your post so that it's readable. Please do so yourself in future.

You're creating multiple Random objects in a loop, which is never a good idea. If you must do that then you should use the constructor that takes a seed and make sure that the seed is unique every time. As it is you are using the system time as a seed which may not have changed sufficiently between iterations to generate a different pseudo-random sequence. Ideally you would use just one Random object, which means using a variable declared outside any methods.
 
Back
Top