How to Generate a Random Alphanumeric String

Save

Most programming languages include a random number generator. In the Visual Studio languages (Visual Basic, C# and managed C++), this is the System.Random class. Like most such generators, this class automatically generates its first random number using a function based on the current time. Subsequent numbers are produced using a math function that is known to create statistically valid random numbers. Programmers use random numbers to create security codes, generate encryption keys, drive business simulations and introduce random events into video games.

Things You'll Need

  • Microsoft Visual Basic Express or Visual Studio (2005 version or later)
  • Create a new console application. Open Visual Basic Express or Visual Studio. Click "File," "New Project." When the new project form appears, choose "Visual Basic" then "Windows" from the tree view, choose the "Console Application" icon. Click "OK." After a short wait, the code template for a new console application will appear.

  • Create a function called NewString immediately after the Module declaration as follows:

    Module Module1

    Function NewString(ByVal len As Integer) As String

    The NewString function returns a sequence of random letters and numbers. The value passed to the function determines the length of the string returned.

  • Declare a few memory variables used within the function.

    Dim chars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

        Dim randomizer As New Random

    Dim s As String = ""

    Dim i As Integer

    The variable chars contains all of the possible letters and numbers that may appear in the new string. There are several ways that characters could be generated, but this is as simple and straightforward as any of the alternatives.

    Randomize is a new instance of the random number class. Because it is a class, the new operator must be used here to load its fields and properties into memory and automatically call its constructor to seed the random number generator.

    The variable s will hold the new string as it is built and I is an index into the chars array.

  • Create the loop that generates the random sequence.

        While s.Length < len

    i = randomizer.Next(chars.Length)

    s &= chars(i)

    End While

    On the first repetition of the loop, I gets the next random number from randomizer. Since the length of the chars string is 36, the number returned will range from 0 to 35. The second line inside the loop gets a single character using the index I from the chars string then appends it into s. This loop repeats until the string s contains the desired number of random characters.

  • Return the string s then end the function.

    Return s

    End Function
  • Add code into the main method to test the function.

    Sub Main()

    Console.WriteLine(NewString(10))

    End Sub

    End Module

    When the program is run, a 10-character string is written to the console. Click F5 to run the program

Tips & Warnings

  • To increase the probability that a number appears, repeat the numbers 0 to 9 at the end of the chars string.

References

Promoted By Zergnet

Comments

You May Also Like

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!