|
-
Oct 11th, 2012, 01:24 PM
#1
Thread Starter
Addicted Member
How can I generate random alphanumeric codes?
Hi everyone,
I've been lurking here for a while doing a lot of reading before asking my first question (yes, I'm a noob). Also english is not my native language, so I hope I can explain myself properly.
Ok, my first question is: how can I generate, say 100 random alphanumeric codes that can also be validated later?
Some context: A friend and I have a small "home" brewery and for marketing purposes we're starting a "contest" (may not be the right word I'm looking for, as it's not a competition) where each bottle has one of these alphanumeric codes printed in the inner part of the top (the thing that keeps the bottle unopened, don't know the word). This alphanumeric code is then entered into a web app which validates it and then tells you if you entered a winning code, which earns you prizes like a free six-pack, 12-pack or others.
These alphanumeric codes would be about 8 to 10 characters long and would look something like this: "W4MP6HZS".
These 100 codes need to be generated only once and randomly, and I need to find a way to validate them once they're entered into the web app. I think I can handle most of the programming, but I want to ask you more experienced guys how would you do it (the code generating/validating part)?
Should I just generate 100 random codes, store them in a database and validate them checking if what's entered in the web app matches something in the database or is there a more efficient way to do this?
Cheers.
-
Oct 11th, 2012, 02:09 PM
#2
Re: How can I generate random alphanumeric codes?
marketing purposes we're starting a "contest"
The word you're wanting here is "promotion" and
the thing that keeps the bottle unopened
the word you're wanting here is "cap", "lid", or "bottle top". However for the question at hand, I believe that you're on the right track. Basically what you're wanting to create is a key generator. A simple google seach "Keygen Vb.net" will yeild you alot of results. Preform your keygen and store the values in a database so that you may access them later.
-
Oct 11th, 2012, 02:27 PM
#3
Re: How can I generate random alphanumeric codes?
Here's a simple method that will generate codes of a length you specify. In the example here I'm generating 100 codes that are 14 characters long.
vb.net Code:
Public Shared rand As New Random()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim codes = GetCodesList(14, 100)
End Sub
Private Function GetCodesList(codeLength As Integer, numberOfCodes As Integer) As List(Of String)
Dim codes As New List(Of String)
Do While codes.Count < numberOfCodes
Dim code = GenerateCode(codeLength)
If Not codes.Contains(code) Then codes.Add(code)
Loop
Return codes
End Function
Private Function GenerateCode(codeLength) As String
Const c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
Return New String(Enumerable.Repeat(c, codeLength).Select(Function(s) s(rand.Next(s.Length))).ToArray())
End Function
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Oct 11th, 2012, 02:37 PM
#4
Re: How can I generate random alphanumeric codes?
A fun little project would be to further this by printing out a barcode that you can scan in and query the database with. But that's another project for another time.
-
Oct 11th, 2012, 03:04 PM
#5
Re: How can I generate random alphanumeric codes?
hmmm... OK... I'll bite... just out of curiosity, how do you get the barcode to fit on the inside of a bottle cap? efficiently enough that it is still usable? I'm just asking... And if you're going to that far with it... how about a QR code that the consumer can then scan with their phone... now, get THAT on the inside of a bottle cap and you may have something...
-tg
-
Oct 11th, 2012, 03:55 PM
#6
Thread Starter
Addicted Member
Re: How can I generate random alphanumeric codes?
 Originally Posted by dday9
The word you're wanting here is "promotion" and the word you're wanting here is "cap", "lid", or "bottle top". However for the question at hand, I believe that you're on the right track. Basically what you're wanting to create is a key generator. A simple google seach "Keygen Vb.net" will yeild you alot of results. Preform your keygen and store the values in a database so that you may access them later.
Thanks for the tips dday9.
 Originally Posted by MattP
Here's a simple method that will generate codes of a length you specify. In the example here I'm generating 100 codes that are 14 characters long.
vb.net Code:
Public Shared rand As New Random()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim codes = GetCodesList(14, 100)
End Sub
Private Function GetCodesList(codeLength As Integer, numberOfCodes As Integer) As List(Of String)
Dim codes As New List(Of String)
Do While codes.Count < numberOfCodes
Dim code = GenerateCode(codeLength)
If Not codes.Contains(code) Then codes.Add(code)
Loop
Return codes
End Function
Private Function GenerateCode(codeLength) As String
Const c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
Return New String(Enumerable.Repeat(c, codeLength).Select(Function(s) s(rand.Next(s.Length))).ToArray())
End Function
This has been very helpful in figuring out how I want to generate the codes. Thanks!
 Originally Posted by formlesstree4
A fun little project would be to further this by printing out a barcode that you can scan in and query the database with. But that's another project for another time.
 Originally Posted by techgnome
hmmm... OK... I'll bite... just out of curiosity, how do you get the barcode to fit on the inside of a bottle cap? efficiently enough that it is still usable?  I'm just asking... And if you're going to that far with it... how about a QR code that the consumer can then scan with their phone... now, get THAT on the inside of a bottle cap and you may have something...
-tg
Well those are interesting projects for the future indeed.
-
Oct 11th, 2012, 04:21 PM
#7
Re: How can I generate random alphanumeric codes?
 Originally Posted by techgnome
hmmm... OK... I'll bite... just out of curiosity, how do you get the barcode to fit on the inside of a bottle cap? efficiently enough that it is still usable?  I'm just asking... And if you're going to that far with it... how about a QR code that the consumer can then scan with their phone... now, get THAT on the inside of a bottle cap and you may have something...
-tg
Wide-mouthed bottles for big-mouthed consumers.
My usual boring signature: Nothing
 
-
Oct 11th, 2012, 09:05 PM
#8
Re: How can I generate random alphanumeric codes?
 Originally Posted by techgnome
hmmm... OK... I'll bite... just out of curiosity, how do you get the barcode to fit on the inside of a bottle cap? efficiently enough that it is still usable?  I'm just asking... And if you're going to that far with it... how about a QR code that the consumer can then scan with their phone... now, get THAT on the inside of a bottle cap and you may have something...
-tg
Oh hell! I didn't know the numbers and stuff would be going inside the cap. Ignore me then!
-
Oct 11th, 2012, 09:53 PM
#9
Re: How can I generate random alphanumeric codes?
Sorry that must have been my inner business analyst ... I picked up on the "each bottle has one of these alphanumeric codes printed in the inner part of the top" requirement... 
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|