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.

3
  • Can you add a sample of what your expected output of this function should be? Commented Sep 7, 2013 at 7:43
  • i want that function should return 123456 and some times 123ABC and so on all randoms Commented Sep 7, 2013 at 7:45
  • do u understand the requirements Commented Sep 7, 2013 at 7:49

1 Answer 1

1

The first problem to solve is the random number generator. You should use only one instance and not multiple instances that gives back the same sequence if called in short time distance. Then it is difficult to say what 'something' means in your requirements, but supposing you are fine with rougly 70% numbers and 30% a mix of numbers and strings then you call the random generator to decide for a sequence of only numbers or a mixed one. Based on the output of the random selection of the sequence build from the appropriate string

' 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.