Results 1 to 15 of 15

Thread: serial number generator program

Hybrid View

  1. #1
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: serial number generator program

    Not sure if this improves the speed of MarkT's code, but it does seem to knock out 4 looping iterations:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        MsgBox GenerateSerialNumber
    End Sub
    
    Private Function GenerateSerialNumber() As String
    Dim strChar(3) As String, i As Integer
    Randomize
    For i = 0 To 3
        If i < 3 Then
            strChar(i) = Chr(Int(Rnd() * 26) + 65)
        Else
            strChar(i) = Format$(Int(Rnd() * 100000), "00000")
        End If
    Next i
    GenerateSerialNumber = Join(strChar, "")
    End Function
    Doctor Ed

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: serial number generator program

    Quote Originally Posted by Code Doc View Post
    Not sure if this improves the speed of MarkT's code, but it does seem to knock out 4 looping iterations:
    I thought about doing it that way but decided to do a random digit as each spot. If you just do one random number for the last five digit you will end up with a lot of low numbers that will have a number of zeros padding them.

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: serial number generator program

    Quote Originally Posted by MarkT View Post
    I thought about doing it that way but decided to do a random digit as each spot. If you just do one random number for the last five digits you will end up with a lot of low numbers that will have a number of zeros padding them.
    Hmmm... I generated 100,000 random 5-digit numbers this way several times and the average is always very near 50,000 and sometimes over 50,000. To me, that indicates no small number bias whatsoever. Give it a try:
    Code:
    Private Sub Command1_Click()
    Dim Sum As Single, Num As Long
    Num = 100000
    For i = 1 To Num
        Call GenerateSerialNumber
        Sum = Sum + CSng(Right$(GenerateSerialNumber, 5))
    Next
    MsgBox Format$(Sum / Num, "##,###.##")
    End Sub
    
    Private Function GenerateSerialNumber() As String
    Dim strChar(3) As String
    For i = 0 To 3
        If i < 3 Then
            strChar(i) = Chr(Int(Rnd() * 26) + 65)
        Else
            strChar(i) = Format$(Int(Rnd() * 100000), "00000")
        End If
    Next i
    GenerateSerialNumber = Join(strChar, "")
    End Function
    
    Private Sub Form_Load()
    Randomize
    End Sub
    If a distribution of random 5-digit numbers were biased toward small numbers (and thus not random), would not the mean of the distribution have to be significantly less than the theoretical mean of 50,000?
    Last edited by Code Doc; Jul 15th, 2010 at 02:53 PM.
    Doctor Ed

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: serial number generator program

    Well I'd probably use something more like:
    Code:
    Option Explicit
    
    Private Type GUID
        Data1_Lo As Integer
        Data1_Hi As Integer
        Data2 As Integer
        Data3 As Integer
        Data4_0To3 As Long
        Data4_4To7 As Long
    End Type
    
    Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As Long
    
    Private Function GenSerial() As String
        Dim udtGUID As GUID
        Dim intA As Integer
        
        If CoCreateGuid(udtGUID) Then
            GenSerial = "*error*"
        Else
            intA = Asc("A")
            With udtGUID
                GenSerial = _
                        Chr$(intA + (.Data1_Lo And &H7FFF) Mod 26) _
                      & Chr$(intA + (.Data2 And &H7FFF) Mod 26) _
                      & Chr$(intA + (.Data3 And &H7FFF) Mod 26) _
                      & Format$((.Data4_4To7 And &H7FFFFFFF) Mod 100000, "00000")
            End With
        End If
    End Function
    
    Private Sub cmdGenerate_Click()
        txtSerial.Text = GenSerial()
    End Sub
    Last edited by dilettante; Jul 15th, 2010 at 06:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width