我需要一个随机数字和字母生成器代码

时间:2014-01-02 16:56:29

标签: c# string random generator serial-number

所以我想找到合适的代码来生成类似代码:61NS4-AGHLE-1AJP6-KA2AY

我在很久以前编码的旧VB程序中使用了正确的代码。也许任何人都可以建议一些有用:)

 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

我不记得它曾经如何运作:/。

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

这应该可以帮助您生成可变长度令牌

    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();
    }

当然可以添加。 - 你喜欢的字符串之间的_

答案 2 :(得分:1)

尝试:http://www.codeproject.com/Articles/423229/CsharpRandomStringGenerator这对我来说一直很有用

同时查看旧主题:How can I generate random alphanumeric strings in C#?

只需修改20char&amp;根据需要添加' - '