Results 1 to 12 of 12

Thread: [RESOLVED] How i generate unique id ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2011
    Posts
    151

    Resolved [RESOLVED] How i generate unique id ?

    Hello all,

    i'm developing a courses program with database

    name
    certificate id

    i need code to generate unique certificate id like (DE3 VFG Y5U) 9 letters and numbers and i don't need to duplicate this id


    is there's any idea ?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: How i generate unique id ?

    I'm not sure that you'll do better than just creating one and seeing whether or not it exists. It looks like you are allowing 36 characters, so you might create an array with those 36 characters (A-Z and 0-9), then make 9 random picks from that set. Once you have an ID, query the DB to see if it has already been used. Sure, that query will fail occasionally, but you are making up a number that is 36^9, which is a REALLY big number, so collisions will be very rare. If you DO get a collision, just generate a new number and try again.

    The easier way to go is to use a GUID, which pretty much all databases will work with, and for which there is a type in .NET. However, that's a larger ID than the 9 characters you are showing. Whether the convenience of working with a built in type is worth the cost in a larger ID, is up to you.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2011
    Posts
    151

    Re: How i generate unique id ?

    thank you for your reply

    i can use 6 letters and numbers just need acode to generate this id

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: How i generate unique id ?

    Given what Shaggy was suggesting, how would you begin to accomplish this?
    I'll help with a list of steps suggested:
    1. Load letters & numbers into a list (I would suggest a List(Of String) )
    2. Pick 9 random values from said list (hint you can use a loop and Random.Next() to get this)
    3. Check that the generated ID doesn't exist, if it does then either change 1 in it or just generate a whole new one.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: How i generate unique id ?

    well to generate unique Numbers with Letter you could use the Date
    and some Random Letters

    like so..
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dateAndTime As Date
            dateAndTime = Now
            Dim sPrefix As String = ""
            Dim rdm As New Random()
            For i As Integer = 1 To 3 ' 3 Letters enough ?
                sPrefix &= ChrW(rdm.Next(65, 90))
            Next
            Debug.Print(sPrefix & Format(dateAndTime, "yyyyMMddHHmmss").ToString)
        End Sub
    hth
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,836

    Re: How i generate unique id ?

    You didn't mention what database you are using but in MS SQL you may be able to use:

    select newid()

    It generates something like this:

    A79D8796-BF78-4304-B160-297ED3CFFB7A
    Last edited by TysonLPrice; Feb 14th, 2020 at 09:06 AM.
    Please remember next time...elections matter!

  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: How i generate unique id ?

    Here's a quick and dirty class I came up with that generates id's:
    vb Code:
    1. Option Explicit On
    2. Option Strict On
    3. Option Infer Off
    4.  
    5. Public Class GenerateID
    6.  
    7.     Private m_Characters As String
    8.     Private m_IDLength As Integer = 9I
    9.     Private Const m_DefaultChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    10.  
    11.     Public Sub New()
    12.         m_IDLength = 6I
    13.         m_Characters = m_DefaultChars
    14.     End Sub
    15.  
    16.     Public Sub New(Length As Integer)
    17.         Me.IDLength = Length
    18.         m_Characters = m_DefaultChars
    19.     End Sub
    20.  
    21.     Public Sub New(Length As Integer, Characters As String)
    22.         Me.IDLength = Length
    23.         Me.Characters = Characters
    24.     End Sub
    25.  
    26.     Public Property IDLength As Integer
    27.         Get
    28.             Return m_IDLength
    29.         End Get
    30.         Set(value As Integer)
    31.             Select Case True
    32.                 Case value < 0I
    33.                     Throw New ArgumentOutOfRangeException("IDLength must be greater than 0 characters")
    34.                 Case value > 100I
    35.                     Throw New ArgumentOutOfRangeException("IDLength must be less than or equal to 100 characters")
    36.                 Case Else
    37.                     m_IDLength = value
    38.             End Select
    39.         End Set
    40.     End Property
    41.  
    42.     Public Property Characters As String
    43.         Get
    44.             Return m_Characters
    45.         End Get
    46.         Set(value As String)
    47.             If value.Length = 0I Then
    48.                 Throw New ArgumentException("Cannot be blank")
    49.             Else
    50.                 m_Characters = value
    51.             End If
    52.         End Set
    53.     End Property
    54.  
    55.     Public Overloads Function GenerateID() As String
    56.         Return GenerateID(m_IDLength, m_Characters)
    57.     End Function
    58.  
    59.     Public Overloads Function GenerateID(Length As Integer) As String
    60.         Return GenerateID(Length, m_Characters)
    61.     End Function
    62.  
    63.     Public Overloads Function GenerateID(Length As Integer, Chars As String) As String
    64.         Dim Output As String = String.Empty
    65.         Dim Rnd As New Random(Integer.Parse(DateTime.Now.ToString("HHmmssffff")))
    66.  
    67.         For Counter As Integer = 0I To Length - 1I
    68.             Output &= Chars(Rnd.Next(Chars.Count))
    69.         Next Counter
    70.  
    71.         Return Output
    72.     End Function
    73.  
    74. End Class
    And to use it just do this:
    vb Code:
    1. Private m_GenerateID As New GenerateID()
    2. m_GenerateID.GenerateID()
    The class provides code that allows the number of characters in the ID to be changed and the character set to be specified, but the defaults for it are what the OP is looking to do here.
    Also all it does is generates the ID, it doesn't add any group separations to it like a space or hyphen every 3 or 5th character, but that's super easy for him to implement once the ID is generated.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: How i generate unique id ?

    You did not specify .NET or SQL.
    9 digits will have a finite randomness, but anyhow...
    For SQL you can do : select LEFT(NEWID(),8) + (select LEFT(NEWID(),1))
    For .NET you can do: Dim rn = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 9)

    But you will have to check if the id exists in you table before inserting.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: How i generate unique id ?

    how about this....

    Code:
    dim uniqueID as string = Guid.NewGuid().ToString().Replace("-","").Substring(6)
    //freehand code beware of syntax
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: How i generate unique id ?

    That would only give you 16^6 possible ID, which is quite a few, but not as great as the original 36^9.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2011
    Posts
    151

    Re: How i generate unique id ?

    Thank you all
    i appreciate all help

    By the way I use Mysql Database and i will prevent duplication only when save data and it seems that this code will not generate duplicate code

    this is good solutions
    Quote Originally Posted by JuggaloBrotha View Post
    Here's a quick and dirty class I came up with that generates id's:
    vb Code:
    1. Option Explicit On
    2. Option Strict On
    3. Option Infer Off
    4.  
    5. Public Class GenerateID
    6.  
    7.     Private m_Characters As String
    8.     Private m_IDLength As Integer = 9I
    9.     Private Const m_DefaultChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    10.  
    11.     Public Sub New()
    12.         m_IDLength = 6I
    13.         m_Characters = m_DefaultChars
    14.     End Sub
    15.  
    16.     Public Sub New(Length As Integer)
    17.         Me.IDLength = Length
    18.         m_Characters = m_DefaultChars
    19.     End Sub
    20.  
    21.     Public Sub New(Length As Integer, Characters As String)
    22.         Me.IDLength = Length
    23.         Me.Characters = Characters
    24.     End Sub
    25.  
    26.     Public Property IDLength As Integer
    27.         Get
    28.             Return m_IDLength
    29.         End Get
    30.         Set(value As Integer)
    31.             Select Case True
    32.                 Case value < 0I
    33.                     Throw New ArgumentOutOfRangeException("IDLength must be greater than 0 characters")
    34.                 Case value > 100I
    35.                     Throw New ArgumentOutOfRangeException("IDLength must be less than or equal to 100 characters")
    36.                 Case Else
    37.                     m_IDLength = value
    38.             End Select
    39.         End Set
    40.     End Property
    41.  
    42.     Public Property Characters As String
    43.         Get
    44.             Return m_Characters
    45.         End Get
    46.         Set(value As String)
    47.             If value.Length = 0I Then
    48.                 Throw New ArgumentException("Cannot be blank")
    49.             Else
    50.                 m_Characters = value
    51.             End If
    52.         End Set
    53.     End Property
    54.  
    55.     Public Overloads Function GenerateID() As String
    56.         Return GenerateID(m_IDLength, m_Characters)
    57.     End Function
    58.  
    59.     Public Overloads Function GenerateID(Length As Integer) As String
    60.         Return GenerateID(Length, m_Characters)
    61.     End Function
    62.  
    63.     Public Overloads Function GenerateID(Length As Integer, Chars As String) As String
    64.         Dim Output As String = String.Empty
    65.         Dim Rnd As New Random(Integer.Parse(DateTime.Now.ToString("HHmmssffff")))
    66.  
    67.         For Counter As Integer = 0I To Length - 1I
    68.             Output &= Chars(Rnd.Next(Chars.Count))
    69.         Next Counter
    70.  
    71.         Return Output
    72.     End Function
    73.  
    74. End Class
    And to use it just do this:
    vb Code:
    1. Private m_GenerateID As New GenerateID()
    2. m_GenerateID.GenerateID()
    The class provides code that allows the number of characters in the ID to be changed and the character set to be specified, but the defaults for it are what the OP is looking to do here.
    Also all it does is generates the ID, it doesn't add any group separations to it like a space or hyphen every 3 or 5th character, but that's super easy for him to implement once the ID is generated.

    thank you again

  12. #12
    New Member
    Join Date
    Mar 2020
    Posts
    2

    Lightbulb Re: [RESOLVED] How i generate unique id ?

    try this method this works like a charm

    Code:
        Private Sub UnixTimeMethod()
            Dim currentTime As DateTime = DateTime.UtcNow
            Dim StringTime As String = currentTime.ToString
            Dim parsedDateTime As DateTime = DateTime.Parse(StringTime)
            Dim unixTime = (parsedDateTime - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
            Dim utcString As String = unixTime.ToString
    
    
            Dim sPrefix As String = ""
            Dim rdm As New Random()
            For i As Integer = 1 To 3 ' 3 Letters enough ?
                sPrefix &= ChrW(rdm.Next(65, 90))
            Next
            TextBox2.Text = (sPrefix & utcString)
        End Sub
    OUTPUT Examples

    Code:
    GYK1501270543
    
    VWT1501270606
    
    WRH1501270634
    
    SKI1501270648
    
    QXL1501270716

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