Click here to Skip to main content
13,138,372 members (49,618 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


25 bookmarked
Posted 9 Feb 2009

Text Based Random Password Generator

, 18 Feb 2009
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Sean Botha
Web Developer na
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

Jimoh Ajibola Akinyemi10-Mar-09 8:24
memberJimoh Ajibola Akinyemi10-Mar-09 8:24 
Sean Botha11-Mar-09 0:36
memberSean Botha11-Mar-09 0:36 
Generalgood idea Pin
Donsw4-Mar-09 9:37
memberDonsw4-Mar-09 9:37 
GeneralNice Pin
SmirkinGherkin19-Feb-09 2:06
memberSmirkinGherkin19-Feb-09 2:06 
A nice simple solution, well explained through the article.
GeneralRe: Nice Pin
Sean Botha19-Feb-09 2:33
memberSean Botha19-Feb-09 2:33 
Generaljust for the beauty for your article Pin
Jarno Burger18-Feb-09 2:35
memberJarno Burger18-Feb-09 2:35 
GeneralRe: just for the beauty for your article Pin
Sean Botha18-Feb-09 20:07
memberSean Botha18-Feb-09 20:07 
GeneralRe: just for the beauty for your article Pin
Jarno Burger23-Feb-09 5:43
memberJarno Burger23-Feb-09 5: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 18 Feb 2009
Article Copyright 2009 by Sean Botha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid