1

I don't understand how to add numbers to the random string, and instead of it showing a string of like 3 sometimes, I want it to always show a string of 5 and I have no clue how to do that.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim rndnumber As Random
    Dim number As Integer
    rndnumber = New Random
    number = rndnumber.Next(1, 80000)
    TextBox1.Text = number.ToString
End Sub
2
  • Nope, it still generates 4 letters and smaller numbers. Commented Feb 6, 2013 at 7:31
  • Try this: TextBox1.Text = number.ToString().PadLeft(5, '0'). You can also use PadRight depending on whichever you want Commented Feb 6, 2013 at 7:59

3 Answers 3

4

You could use this function to create random strings:

Public Function GenerateRandomString(ByRef len As Integer, ByRef upper As Boolean) As String
    Dim rand As New Random()
    Dim allowableChars() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789".ToCharArray()
    Dim final As String = String.Empty
    For i As Integer = 0 To len - 1
        final += allowableChars(rand.Next(allowableChars.Length - 1))
    Next

    Return IIf(upper, final.ToUpper(), final)
End Function

You can call this function like this:

GenerateRandomString(5, False)

First parameter is number of characters and second is if you want upper case characters or not (True or False).

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but nothing came out of the generate box. I added the function then I went and called the function on the on click generate and tested it but it didn't work.
I tested the code in Visual Studio (Visual Basic.Net) it works you could try like this: Dim text As String text = GenerateRandomString(5, False) Console.WriteLine(text) If you want the numbers in random you could change this line: Dim allowableChars() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789".ToCharArray() to "12313827481274971289418947128471748912741894781" it works.
Random.Next(maxValue As Integer) shouldn't return an Integer less than maxValue? Then allowableChars.Length should be enough and correct, since rand.Next() will always skip the last member of allowableChars otherwise
1

This generates a random string number of length 5:

final = rdm.Next(0, 100000).ToString("00000")

And this function generates a random string of everything of any length:

Public Function GetRandomString(ByVal iLength As Integer) As String
    Dim sResult As String = ""
    Dim rdm As New Random()

    For i As Integer = 1 To iLength
        sResult &= ChrW(rdm.Next(32, 126))
    Next

    Return sResult
End Function

Comments

0

Do you need it to be a string of numbers?, because if not, you could use System.IO.Path.GetRandomFileName. This function gives you a random uppercase string of any length, with a default of eight characters

Public Function GetRandomString(Optional ByVal iLength As Integer = 8) As String
    Dim sPath, SResult As String

    sPath = ""
    Do
        sPath = sPath + System.IO.Path.GetRandomFileName.Replace(".", "")
    Loop Until sPath.Length > iLength
    SResult = sPath.Substring(0, iLength)
    Return SResult.ToUpper
End Function

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.