[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.
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? :confused:
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.
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?
Re: Random/Unique Number Generation?
looking at the existing table 1-2k a year...
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?
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.
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?
Re: Random/Unique Number Generation?
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.
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.
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...
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
Quote:
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
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!
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.