Results 1 to 9 of 9

Thread: How can I generate random alphanumeric codes?

  1. #1

    Thread Starter
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    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.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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:
    1. Public Shared rand As New Random()
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Dim codes = GetCodesList(14, 100)
    5.     End Sub
    6.  
    7.     Private Function GetCodesList(codeLength As Integer, numberOfCodes As Integer) As List(Of String)
    8.         Dim codes As New List(Of String)
    9.         Do While codes.Count < numberOfCodes
    10.             Dim code = GenerateCode(codeLength)
    11.             If Not codes.Contains(code) Then codes.Add(code)
    12.         Loop
    13.         Return codes
    14.     End Function
    15.  
    16.     Private Function GenerateCode(codeLength) As String
    17.         Const c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    18.         Return New String(Enumerable.Repeat(c, codeLength).Select(Function(s) s(rand.Next(s.Length))).ToArray())
    19.     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.

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

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Re: How can I generate random alphanumeric codes?

    Quote Originally Posted by dday9 View Post
    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.

    Quote Originally Posted by MattP View Post
    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:
    1. Public Shared rand As New Random()
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Dim codes = GetCodesList(14, 100)
    5.     End Sub
    6.  
    7.     Private Function GetCodesList(codeLength As Integer, numberOfCodes As Integer) As List(Of String)
    8.         Dim codes As New List(Of String)
    9.         Do While codes.Count < numberOfCodes
    10.             Dim code = GenerateCode(codeLength)
    11.             If Not codes.Contains(code) Then codes.Add(code)
    12.         Loop
    13.         Return codes
    14.     End Function
    15.  
    16.     Private Function GenerateCode(codeLength) As String
    17.         Const c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    18.         Return New String(Enumerable.Repeat(c, codeLength).Select(Function(s) s(rand.Next(s.Length))).ToArray())
    19.     End Function
    This has been very helpful in figuring out how I want to generate the codes. Thanks!

    Quote Originally Posted by formlesstree4 View Post
    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.
    Quote Originally Posted by techgnome View Post
    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.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How can I generate random alphanumeric codes?

    Quote Originally Posted by techgnome View Post
    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

  8. #8

    Re: How can I generate random alphanumeric codes?

    Quote Originally Posted by techgnome View Post
    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!

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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