Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 132,209 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 920 people online right now. Registration is fast and FREE... Join Now!




padright string with random characters (a-z)

 
Reply to this topicStart new topic

padright string with random characters (a-z)

blgtn2000
post 20 Feb, 2008 - 08:51 AM
Post #1


New D.I.C Head

*
Joined: 20 Feb, 2008
Posts: 4

I almost have it...However, using this code, it will add the same letter to equal the 8 character password. For example, if the password is "dog", it will add "eeeee" to the end. If I type "dog" again, it will add "rrrrr" to the end. Is there a way to randomize the letters so they are not the same?



Private Sub xMethod3MenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xMethod3MenuItem.Click
Dim password As String
Dim passwordLength As Integer
Dim pad As String
Dim max As Integer
Dim min As Integer

password = InputBox("Enter a password:", "Password Generator")
password = password.ToUpper
Me.xPasswordTextBox.Text = password

passwordLength = password.Length


max = 121
min = 95
Randomize()
pad = pad & Chr(Int((max - min + 1) * Rnd() + min))


If passwordLength < 8 Then
password = password.PadRight(8, pad)
End If
Me.xNewPasswordAnswerLabel.Text = password

End Sub
End Class


This post has been edited by blgtn2000: 20 Feb, 2008 - 01:24 PM
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 20 Feb, 2008 - 10:51 AM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Moved to VB.NET.

What is the problem that you are having with your code?

We need an accurate description in order to be able to help you.
User is offlineProfile CardPM

Go to the top of the page

blgtn2000
post 20 Feb, 2008 - 01:07 PM
Post #3


New D.I.C Head

*
Joined: 20 Feb, 2008
Posts: 4

I have to generate a password that is 8 characters long. If the password isn't 8 characters, then it's suppose to fill the rest with random letters (a-z) until it's 8 characters.

I am trying to use the PadRight command; however, I don't understand how to create random characters. I know how to randomize numbers, but not letters.

I am stuck on the password = password.PadRight(8, ?)
User is offlineProfile CardPM

Go to the top of the page

blgtn2000
post 20 Feb, 2008 - 01:21 PM
Post #4


New D.I.C Head

*
Joined: 20 Feb, 2008
Posts: 4

QUOTE(blgtn2000 @ 20 Feb, 2008 - 09:51 AM) *

CODE
Private Sub xMethod3MenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xMethod3MenuItem.Click
        Dim password As String
        Dim passwordLength As Integer
        Dim pad As Char

        pad = Convert.ToString("azvwqsty")

        password = InputBox("Enter a password:", "Password Generator")
        password = password.ToUpper
        Me.xPasswordTextBox.Text = password

        passwordLength = password.Length

        If passwordLength < 8 Then
            password = password.PadRight(8, pad)
        End If
        Me.xNewPasswordAnswerLabel.Text = password

    End Sub
End Class


User is offlineProfile CardPM

Go to the top of the page

Sothrie
post 20 Feb, 2008 - 01:46 PM
Post #5


New D.I.C Head

*
Joined: 3 Dec, 2007
Posts: 24


My Contributions


Yeah, you could do that. I'm not sure that PadRight allows you to pad more than one character, but you could just create a subfunction and append a random character to the end until it's the right length. It'd probably look someting like this:

CODE

Private Function PadRightRandom(byval stringToPad as String, byval finalLength as Integer) as String
         Do Until stringToPad.Length >= finalLength
                  Dim pad as String
                  max = 121
                  min = 95
                  Randomize()
                  pad = pad & Chr(Int((max - min + 1) * Rnd() + min))

                  stringToPad &= pad
         Loop
         PadRightRandom = stringToPad
End Sub


And then you'd just call that function where you've been trying to PadRight.

Hope that Helps!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 07:44PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month