1

I want to generate a random number of 6 intergers in vb.net which should be number and also some times alphanumerice.Like ist 4 or 5 numbers should be numbers and next should be alphnumeric.I created both numbers separatedly.like this

Public Function rand() As String
        'Number
        Dim rng As Random = New Random
        Dim number As Integer = rng.Next(1, 1000000)
        Dim digits As String = number.ToString("000000")
        'Alphnumeric
        Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        Dim r As New Random
        Dim sb As New StringBuilder
        For i As Integer = 1 To 5
            Dim idx As Integer = r.Next(0, 35)
            sb.Append(s.Substring(idx, 1))
        Next
    End Function

Now Any One Give Me Idea that in this function can i return number and some times string or may b a single instances which create both numbers and strings.

4

1 に答える 1

1

最初に解決すべき問題は乱数発生器です。短時間で呼び出された場合に同じシーケンスを返す複数のインスタンスではなく、1 つのインスタンスのみを使用する必要があります。次に、要件で「何か」が何を意味するかを言うのは困難ですが、おおよそ70%の数字と30%の数字と文字列の混合で問題ないと仮定すると、乱数ジェネレーターを呼び出して、数字のみのシーケンスまたは混合したもの。適切な文字列からのシーケンス構築のランダム選択の出力に基づく

' A global unique random generator'
Dim rng As Random = New Random
Sub Main
    Console.WriteLine(rand())
    Console.WriteLine(rand())
    Console.WriteLine(rand())
    Console.WriteLine(rand())
End Sub

Public Function rand() As String

    Dim sb As New StringBuilder

    ' Selection of pure numbers sequence or mixed one
    Dim pureNumbers = rng.Next(1,11)
    if pureNumbers < 7 then
        ' Generate a sequence of only digits
        Dim number As Integer = rng.Next(1, 1000000)
        Dim digits As String = number.ToString("000000")
        For i As Integer = 1 To 6
            Dim idx As Integer = rng.Next(0, digits.Length)
            sb.Append(digits.Substring(idx, 1))
        Next
    else
        ' Generate a sequence of digits and letters 
        Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        For i As Integer = 1 To 6
            Dim idx As Integer = rng.Next(0, 36)
            sb.Append(s.Substring(idx, 1))
        Next
    End if 
    return sb.ToString()
End Function
于 2013-09-07T07:59:12.020 に答える