Results 1 to 15 of 15

Thread: serial number generator program

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    1

    serial number generator program

    hello,
    I need some help pls: i want to create a program that will generate a serial number composed of 8 carattercts (3 letters followed by 5 numbers ---> i.e. FRT78136). Unfortunately i don't know how to use visualbasic very well; the last program i learnt was gwbasic...using gwbasic i would have used the randomize function. The program should change the rnd function in accordance to a sspecific yesr (i.e. when i am prompted for the 'randomize seed number' this should be a specific year such as 2010).

    thank you for your kind help
    Debora

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: serial number generator program

    Search the Code Bank section on the forum for "serial generator", and check out Planet Source Code.

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

    Re: serial number generator program

    Real quick sample
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        MsgBox GenerateSerialNumber
    End Sub
    
    Private Function GenerateSerialNumber() As String
    Dim i As Integer
    Dim strChar(7) As String
    
        Randomize
        
        For i = 0 To 7
            If i < 3 Then
                strChar(i) = Chr(Int(Rnd() * 26) + 65)
            Else
                strChar(i) = Int(Rnd() * 10)
            End If
        Next i
        
        GenerateSerialNumber = Join(strChar, "")
    End Function

  4. #4
    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

  5. #5
    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.

  6. #6
    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

  7. #7
    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.

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

    Re: serial number generator program

    So, what is the difference between :

    Format$((.Data4_4To7 And &H7FFFFFFF) Mod 100000, "00000")

    and

    Format$(Int(Rnd() * 100000), "00000")

    I've tried both and the mean of the results of thousands of repetitions looks to be the same --> 50,000.
    Doctor Ed

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: serial number generator program

    No extra variable or loop.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Randomize
    End Sub
    
    Private Sub cmdGetSN_Click()
        txtSN.Text = RndSN()
    End Sub
    
    Function RndSN() As String
        '-- no guaranty SN is unique
        RndSN = Chr$(Int(Rnd * 26) + 65) & Chr$(Int(Rnd * 26) + 65) & _
                Chr$(Int(Rnd * 26) + 65) & Format(Int(Rnd * 100000), "00000")
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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

    Re: serial number generator program

    Quote Originally Posted by anhn View Post
    No extra variable or loop.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Randomize
    End Sub
    
    Private Sub cmdGetSN_Click()
        txtSN.Text = RndSN()
    End Sub
    
    Function RndSN() As String
        '-- no guaranty SN is unique
        RndSN = Chr$(Int(Rnd * 26) + 65) & Chr$(Int(Rnd * 26) + 65) & _
                Chr$(Int(Rnd * 26) + 65) & Format(Int(Rnd * 100000), "00000")
    End Function
    I considered that, but I figured everybody would yell at me for "slow" string concatenation. I thought MarkT's Join() of the simple array might be faster. Maybe Merri can test them.

    Good one, AnHn.
    Doctor Ed

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

    Re: serial number generator program

    Why would speed even be an issue here? Nobody is going to need thousands of these at once.


    I completely missed the "year" requirement. I'm not sure where that comes in.

  12. #12
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: serial number generator program

    Speed is not an issue here but for CodeDoc:
    My function RndSN() is much simpler and about 30&#37; faster than your function GenerateSerialNumber() in Post#6.

    Edit: If you want a faster function (but longer code) then this one is 3 times faster:
    Code:
    Function RndSN2() As String
        '-- no guaranty SN is unique
        Dim n As Long
        RndSN2 = "00000000"
        n = Int(Rnd * 100000)
        Mid$(RndSN2, 8 - Len(n)) = n
        Mid$(RndSN2, 1, 1) = Chr$(65 + Int(Rnd * 26))
        Mid$(RndSN2, 2, 1) = Chr$(65 + Int(Rnd * 26))
        Mid$(RndSN2, 3, 1) = Chr$(65 + Int(Rnd * 26))
    End Function
    Last edited by anhn; Jul 15th, 2010 at 08:47 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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

    Re: serial number generator program

    Quote Originally Posted by anhn View Post
    Speed is not an issue here but for CodeDoc:
    My function RndSN() is much simpler and about 30&#37; faster than your function GenerateSerialNumber() in Post#6.

    Edit: If you want a faster function (but longer code) then this one is 3 times faster:
    Code:
    Function RndSN2() As String
        '-- no guaranty SN is unique
        Dim n As Long
        RndSN2 = "00000000"
        n = Int(Rnd * 100000)
        Mid$(RndSN2, 8 - Len(n)) = n
        Mid$(RndSN2, 1, 1) = Chr$(65 + Int(Rnd * 26))
        Mid$(RndSN2, 2, 1) = Chr$(65 + Int(Rnd * 26))
        Mid$(RndSN2, 3, 1) = Chr$(65 + Int(Rnd * 26))
    End Function
    Truly exceptional. Mid$() replace blows both simple string concatenation and Join() away. Not sure exactly why but it does.

    Dilettante said, "I completely missed the "year" requirement. I'm not sure where that comes in."
    --------------
    Neither do I. I'm not sure what that means to the problem. Perhaps it has something to do wth seeding Randomize, but the whole concept seemed nebulous. So, I did not address it either.
    Last edited by Code Doc; Jul 16th, 2010 at 07:31 PM.
    Doctor Ed

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

    Re: serial number generator program

    Maybe a new repeating series each year?

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

    Re: serial number generator program

    To me it just looks like the OP was using the year as the seed for the Rnd or Randomize seed.

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