Click here to Skip to main content
15,558,672 members
Articles / Programming Languages / Visual Basic
Posted 9 Feb 2009

Tagged as


26 bookmarked

Text Based Random Password Generator

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
18 Feb 2009CPOL2 min read
An article that demonstrates how to use a list of words to generate a user-friendly random password.


My clients always complain about the random passwords I send them. Even though they have to change the password the first time they log in, they find it difficult to work with the normal random created text that I provide (e.g., zafkpr9b). So, I finally gave in, and created a password generator using a list of words from a text document.

The Problem

The passwords we will be generating will consist of two words and a number. The words will be loaded from a text document with a list of words. You can find a wordlist using Google. There is a nice English word list at Curlew Communications.

This means we need to do four things:

  • Retrieve the list of words
  • Generate the two words
  • Generate the number
  • Add it all together

Retrieve the List of Words

Before I can do anything, I need a list of words. I use the text field parser to loop through all the fields in the text file. This example will work with comma delimited and "word per line" documents.

Dim WordList As New List(Of String)
If My.Computer.FileSystem.FileExists(Filename) Then
    Dim fields As String()
    Dim delimiter As String = ","
    Using parser As New TextFieldParser(Filename)
        While Not parser.EndOfData
            fields = parser.ReadFields()
            For Each word As String In fields
        End While
    End Using

    Throw New Exception(Filename & " doesn't Exist!")
End If

Generate the Two Words

To generate the number, I simply call a method that creates a number less than the number of words I retrieved. Next, I select the nth word from the list and use it.

Dim Position As New Random(System.DateTime.Now.Millisecond)
Dim wordnumber As Integer = Position.Next(0, WordList.Count - 1)
Dim word As String = WordList(wordnumber)
Return word

Generate the Number

I need a number between 0 and 99 to add to the two words I retrieved above. Therefore, I simply generate a random number between 0 and 99.

Dim RandomClass As New Random(System.DateTime.Now.Millisecond)
Dim rndNumber As Integer = RandomClass.Next(0, 99)    

Add it all Together

When adding the words and number together, I first need to know in what order the password should be. To do this, I create another random number and use this number to determine the position of the various words.

Dim PasswordPosition As Integer = RandomClass.Next(0, 5)
Select Case PasswordPosition
    Case 0 : Return word1 & word2 & rndNumber
    Case 1 : Return rndNumber & word1 & word2
    Case 2 : Return word1 & rndNumber & word2
    Case 3 : Return word2 & word & rndNumber
    Case 4 : Return rndNumber & word2 & word1
    Case 5 : Return wood2 & rndNumber & word1
    Case Else : Return word1 & word2
End Select

I also use the Mixedcase method that simply returns the word supplied with its first character capitalized.

Private Function Mixedcase(ByVal Word As String) As String
    If Word.Length = 0 Then Return Word
    If Word.Length = 1 Then Return UCase(Word)
    Return Word.Substring(0, 1).ToUpper & Word.Substring(1).ToLower
End Function

The Final Result

Instead of receiving a password that makes no sense, we can now supply our clients/users a password that is easy to read and understand. Here are a few passwords that the class generated:

  • Orange28Wattmeter
  • 23GreenWaitress
  • AardvarkRed5


This code can be used to automatically generate a password for users that register on a website or anywhere you need a password that is automatically generated.


  • 9th February, 2009: Initial post
  • 18th February, 2009: Added The Final Result section.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer na
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Dobrobit10-Mar-09 9:24
Dobrobit10-Mar-09 9:24 
Sean Botha11-Mar-09 1:36
Sean Botha11-Mar-09 1:36 
Generalgood idea Pin
Donsw4-Mar-09 10:37
Donsw4-Mar-09 10:37 
GeneralNice Pin
SmirkinGherkin19-Feb-09 3:06
SmirkinGherkin19-Feb-09 3:06 
A nice simple solution, well explained through the article.
GeneralRe: Nice Pin
Sean Botha19-Feb-09 3:33
Sean Botha19-Feb-09 3:33 
Generaljust for the beauty for your article Pin
Jarno Burger18-Feb-09 3:35
Jarno Burger18-Feb-09 3:35 
GeneralRe: just for the beauty for your article Pin
Sean Botha18-Feb-09 21:07
Sean Botha18-Feb-09 21:07 
GeneralRe: just for the beauty for your article Pin
Jarno Burger23-Feb-09 6:43
Jarno Burger23-Feb-09 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.