Results 1 to 15 of 15

Thread: [RESOLVED] Random/Unique Number Generation?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] Random/Unique Number Generation?

    Hey,
    I am currently developing a gift certificate generating app. The gift certificate records are going to be stored in SQL database. The company currently just increments the GC number each time one is printed. Obviously that is a terrible way to generate the number. I would like to randomize the number generation but ofcourse I can't have the same number generated that has already been issued. I am basically looking for ideas on how to approach this. Thanks for any input.

  2. #2
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Random/Unique Number Generation?

    I'm curious as to why you feel incrementing the number each time one is made is a terrible idea?
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Random/Unique Number Generation?

    Because of the way they are redeemed... if the users knew of the method then they could easily forge them.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Random/Unique Number Generation?

    Phew....this could be interesting.

    When you say "can not be duplicated", I'm assuming you mean from now until the end of time, right? In other words, the gift cert number will be used once, and never, ever again?

    How many gift certs (ball park figure) does this company generate in any 12 month calendar year?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Random/Unique Number Generation?

    looking at the existing table 1-2k a year...

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Random/Unique Number Generation?

    How many characters is a typical gift certicate number?

    Are they all numeric or a mixture of alpha and numeric?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Random/Unique Number Generation?

    Currently the company uses a 6 digit numeric number that is incremented for each GC. I have no problem using alph numeric characters tho.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Random/Unique Number Generation?

    I have an idea swirling around in my head, although I've no idea if it would work for you.

    Anyway, right now as we speak, how many gift certificate numbers do you have in your SQL database?

  9. #9
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random/Unique Number Generation?

    How about using a GUID?

    Here is an example:
    http://www.codeproject.com/useritems...que_values.asp

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Random/Unique Number Generation?

    Quote Originally Posted by Hack
    I have an idea swirling around in my head, although I've no idea if it would work for you.

    Anyway, right now as we speak, how many gift certificate numbers do you have in your SQL database?
    13k records in this table...

    Hassan thanks for the link tho I don't see how that would work in comparison to the numbers that are already in my database... maybe I am not lookin at it correctly.

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Random/Unique Number Generation?

    Quote Originally Posted by Besoup
    Hassan thanks for the link tho I don't see how that would work in comparison to the numbers that are already in my database... maybe I am not lookin at it correctly.
    His idea is actually not a bad one.

    I assume that the field storing these is setup to not allow duplicates.

    Anyway, my idea is something like this and is uses the numbers you already have.

    You go out and grad a record at randem (this would be easier if you have an IDENTITY field setup) and pull the existing gift cert number.

    Lets say, it is 459132

    Now, you simply take that string and randomize it, or rearrange its order, to come up with a completely different six digit number that is made of the existing numbers. I have no idea how many 6 digit possibilities there are for 459132, but I image there would be a lot.

    The next time you need another number you randemly grab a new gift cert number and do the same thing.

  12. #12
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Random/Unique Number Generation?

    Actually i did a similar thing for one of my projects.

    The theory was... : create 3 listboxes

    1: Generate your certificate numbers, for eg in a listbox1..

    then do a loop where for eg.

    Code:
    dim x as integer
    dim z as integer
    
    for x = 0 to listbox1.listcount -1
    listbox1.listindex = x
    
    listbox2.additem listbox1.text
    
         for z = 0 to listbox2.listcount -1
               listbox2.listindex = z
              if listbox2.text = listbox1.listindex then etc....
         next z
    
    next x
    thats just part of an example.. the theory is:

    GENERATE the code...

    Check it against the populated code,

    once separated any duplicates do same but check with the ones in database...
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  13. #13
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random/Unique Number Generation?

    Quote Originally Posted by Besoup
    Hassan thanks for the link tho I don't see how that would work in comparison to the numbers that are already in my database... maybe I am not lookin at it correctly.
    The MSDN states
    What If Visual Basic Runs Out of GUIDs?

    This is not a problem we need to worry about in our lifetimes. The algorithm that generates GUIDs would allow you to compile several new versions of your component every second for centuries — without repeating or colliding with GUIDs generated by other developers.
    So the chances are so small that you would repeat a GUID. Also you can always just verify your list everytime you create one.

    Create a form, add a command button and put this code in your form to see what a GUID looks like and see how it is unique

    Code:
    Private Declare Function CoCreateGuid_Alt Lib "OLE32.DLL" Alias "CoCreateGuid" (pGuid As Any) As Long
    Private Declare Function StringFromGUID2_Alt Lib "OLE32.DLL" Alias "StringFromGUID2" (pGuid As Any, ByVal address As Long, ByVal Max As Long) As Long
    
    Private Sub Command1_Click()
    
    MsgBox CreateGUID
    
    End Sub
    
    Function CreateGUID() As String
        Dim res As String, resLen As Long, guid(15) As Byte
        res = Space$(128)
        CoCreateGuid_Alt guid(0)
        resLen = StringFromGUID2_Alt(guid(0), ByVal StrPtr(res), 128)
        CreateGUID = Left$(res, resLen - 1)
    End Function

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Random/Unique Number Generation?

    Hassan thanks for the help, I am planning on using the first link you had posted just because the string looks cleaner...

    thanks all!
    Last edited by Besoup; Feb 27th, 2007 at 01:27 PM.

  15. #15
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random/Unique Number Generation?

    Quote Originally Posted by Besoup
    Hassan thanks for the help, I am planning on using the first link you had posted just because the string looks cleaner...

    thanks all!
    My pleasure Besoup.

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