0

So I am trying to find the right code to generate this-like codes: 61NS4-AGHLE-1AJP6-KA2AY

I have the right code in an old VB program I coded ages ago. maybe anyone could suggest something useful. :)

 Public Function generatecode() As Object
    Dim obj8 As Object
    Dim obj11 As Object
    Dim obj6 As Object = "QWEYXWQ10203451QWEYXKP067088090ABCDEFGXDRHJKUPJAQWWXY"
    Dim left As Object = Strings.Len(RuntimeHelpers.GetObjectValue(obj6))
    Dim limit As Object = 5
    VBMath.Randomize()
    Dim obj7 As Object = ""
    If ForLoopControl.ForLoopInitObj(obj8, 1, limit, 1, obj11, obj8) Then
        Do
            '   Dim objectValue As Object = RuntimeHelpers.GetObjectValue(Conversion.Int(Operators.AddObject(Operators.MultiplyObject(left, VBMath.Rnd), 1)))
            '  obj7 = Operators.ConcatenateObject(obj7, Strings.Mid(Conversions.ToString(obj6), Conversions.ToInteger(objectValue), 1))
        Loop While ForLoopControl.ForNextCheckObj(obj8, obj11, obj8)
    End If
    Return RuntimeHelpers.GetObjectValue(obj7)
End Function

Private Function GenerateString(ByVal length As Integer, ByVal content As Integer, ByVal casing As Integer) As String


    ' the random generators
    Dim r, r1, r2 As New Random

    'the final generated string
    Dim gString As String = String.Empty

    Dim LowAlph As String = "abcdefghijklmnopqrstuvwxyz"
    Dim UppAlph As String = LowAlph.ToUpper

    Dim addLetter As Char

    Do Until gString.Length = length
        Select Case casing
            Case 0
                Select Case r.Next(0, 2)
                    Case 0
                        addLetter = LowAlph.Substring(r1.Next(0, 25), 1)
                    Case 1
                        addLetter = UppAlph.Substring(r1.Next(0, 25), 1)
                End Select
            Case 1
                addLetter = LowAlph.Substring(r1.Next(0, 25), 1)
            Case 2
                addLetter = UppAlph.Substring(r1.Next(0, 25), 1)
        End Select
        'add the next character to "gString"
        Select Case content
            Case 0
                Select Case r.Next(0, 2)
                    Case 0
                        gString &= addLetter
                    Case 1
                        gString &= r1.Next(TextBox6.Text, TextBox5.Text) ' (0, 9)
                    Case 2
                        gString &= addLetter
                End Select
            Case 1
                gString &= r1.Next(TextBox6.Text, TextBox5.Text)    '(0, 9)
            Case 2
                gString &= addLetter
        End Select
    Loop

    Return gString

End Function
Function RandomString(cb As Integer) As String

    Randomize()
    Dim rgch As String
    rgch = "abcdefghijklmnopqrstuvwxyz"
    rgch = rgch & UCase(rgch) & "0123456789"

    Dim i As Long
    For i = 1 To cb
        RandomString = RandomString & Mid$(rgch, Int(Rnd() * Len(rgch) + 1), 1)
        RandomString = textbox1.text
    Next

End Function

Public Function RandomString( _
ByVal length As Long, _
Optional charset As String = "abcdefghijklmnopqrstuvwxyz0123456789" _
) As String
    Dim chars() As Byte, value() As Byte, chrUprBnd As Long, i As Long
    If length > 0& Then
        Randomize()
        '     chars = charset
        chrUprBnd = Len(charset) - 1&
        length = (length * 2&) - 1&
        ReDim value(length)
        For i = 0& To length Step 2&
            value(i) = chars(CLng(chrUprBnd * Rnd) * 2&)
        Next
    End If
    ' RandomString = value
End Function

I don't remember anymore how it used to work :/.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
user3002135
  • 237
  • 1
  • 4
  • 15
  • 2
    Is this for the purpose of adding software registration? If that is the case, the codes will need to be generated with a specific formula in mind so they can be verified when typed in correctly. – Lee Harrison Jan 02 '14 at 16:59
  • @LeeHarrison yes it's kind of like that but i just want a few codes generated first. :) – user3002135 Jan 02 '14 at 17:01
  • 3
    Post the code you have or Google it. You're expected to demonstrate a minimal understanding of what's going on and then post problems here. You don't have a problem, you just want someone to do your work for you. – mason Jan 02 '14 at 17:01
  • 1
    Check out this old thread: http://stackoverflow.com/questions/1344221/how-can-i-generate-random-8-character-alphanumeric-strings-in-c just modify the logic for 20 char – Richard Dewhirst Jan 02 '14 at 17:04
  • 1
    @msm8bball you are right, it sounds like that... im going to put it into my question. – user3002135 Jan 02 '14 at 17:04
  • @RichardDewhirst oh thanks! i guess that will help me a lot! thank you. – user3002135 Jan 02 '14 at 17:07
  • Thanks, that looks better. Although if I make a suggestion to improve your code: don't name variables like obj1 obj2 etc. It's very confusing. – mason Jan 02 '14 at 18:38
  • @msm8bball um i got it yesterday, using the link to an older post Richard Dewhirst suggested. thanks anyways :) – user3002135 Jan 03 '14 at 13:18

3 Answers3

2

You could look into the C# Random class:

http://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx

Submersed
  • 8,810
  • 2
  • 30
  • 38
  • oh totally forgot about that, thanks :) – user3002135 Jan 02 '14 at 17:00
  • Then use modulus on randomly generated number to get a character in the Alpha-numeric range of ASCII (48-57 for numbers, 65-90 for capital letters). That should give you a decent distribution for codes. – dkoch74 Jan 02 '14 at 17:04
2

This should be able to help you in case of generating variable length tokens

    private static Random random = new Random();

    public static string RandomToken(int length, string characterSet = "abcdefghijklmnopqrstuvwxyzABCDDEFGHIJKLMNOPQRSTUVWXYZ")
    {
        var builder = new StringBuilder();
        while(builder.Length < length) 
        {
            builder.Append(characterSet.ToCharArray()[random.Next(characterSet.Length)]);
        }
        return builder.ToString();
    }

of course you can add . - _ in between the string as you like

GFoley83
  • 3,439
  • 2
  • 33
  • 46
Shoaib Shaikh
  • 4,565
  • 1
  • 27
  • 35
1

Try: http://www.codeproject.com/Articles/423229/CsharpRandomStringGenerator this has always worked for me before

Also check out old thread : How can I generate random alphanumeric strings in C#?

Just Modify for 20char & add '-' as required

Community
  • 1
  • 1
Richard Dewhirst
  • 229
  • 7
  • 20