5 Replies - 26610 Views - Last Post: 13 January 2009 - 02:12 PM Rate Topic: -----

#1 Craftsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-January 09

Generate Random Alpha-Numeric string 18 characters long

Post icon  Posted 12 January 2009 - 04:40 PM

I nedd some help in generating a random alph-numeric password for a form that I building. The form should suggest a password that 18 characters long. The user would either agree to this password or enter their own password. This is my code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	  Randomize()
	  TextBox1.Text = Int(Rnd() * 18) + 1

   End Sub


When I use this I get a two digit number - All help is appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Generate Random Alpha-Numeric string 18 characters long

Posted 12 January 2009 - 05:41 PM

In order to get the length you want you're going to have to use a loop, probably a For loop. Since the code you have shown is VB.NET code you don't want to use Randomize, it's a legacy function left over from the VB6 (and older) days, stick with the Random Class, which is native to the .Net Library.

You can make your function reusable by having it accept an argument for how long the string needs to be, and whether to return in all caps or not, here's an example.

Here we create an instance of the Random class, then we create a char array containing the alphabet (upper and lower case) and the numbers 0 through 9. We then loop the number of times specified by the len parmeter the function expects. From there we select the index of the array, which will be generated by using the Next method of the Random class

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



Hope this helps :)

EDIT: Moved to VB.NET :)
Was This Post Helpful? 1

#4 BillBear  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-January 09

Re: Generate Random Alpha-Numeric string 18 characters long

Posted 12 January 2009 - 06:04 PM

View PostCraftsman, on 12 Jan, 2009 - 03:40 PM, said:

I nedd some help in generating a random alph-numeric password for a form that I building. The form should suggest a password that 18 characters long. The user would either agree to this password or enter their own password. This is my code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	  Randomize()
	  TextBox1.Text = Int(Rnd() * 18) + 1

   End Sub


When I use this I get a two digit number - All help is appreciated.

Thanks



Try this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x as Integer
Randomize()

For x = 1 to 18
TextBox1.Text = TextBox1.Text & Int(Rnd() * 10)
Next x

End Sub
Was This Post Helpful? 0
  • +
  • -

#5 Craftsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-January 09

Re: Generate Random Alpha-Numeric string 18 characters long

Posted 13 January 2009 - 12:41 PM

Thanks - But, but now I cannot get the function to display in my textbox - I used your code:

Public Class Form1
   Public Function GenerateRandomString(ByRef len As Integer, ByRef upper As Boolean) As String
	  Dim rand As New Random()
	  Dim allowableChars() As Char = "abcdefghighlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray()
	  Dim final As String = String.Empty

	  For i As Integer = 0 To 18 - 1
		 final += allowableChars(rand.Next(allowableChars.Length - 1))

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

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	  GenerateRandomString = TextBox1.Text





   End Sub
End Class


What am I doing wrong? I cannot find anything in the books.
Was This Post Helpful? 0
  • +
  • -

#6 Craftsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-January 09

Re: Generate Random Alpha-Numeric string 18 characters long

Posted 13 January 2009 - 01:04 PM

Thanks Dude - I finally got it. I will post the code as soon as I work out the rest.

Thanks again!! :D
Was This Post Helpful? 0
  • +
  • -

#7 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: Generate Random Alpha-Numeric string 18 characters long

Posted 13 January 2009 - 02:12 PM

i made a correction and some slight changes. the correction was to allow the last character to be part of the random selection. i also made upper optional.

	Public Function GenerateRandomString(ByRef lenStr As Integer, Optional ByVal upper As Boolean = False) As String
		'use
		'TextBox1.Text = GenerateRandomString(18)
		Dim rand As New Random()
		Dim allowableChars() As Char = _
				"abcdefghighlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray()
		Dim final As New System.Text.StringBuilder
		Do
			'final += allowableChars(rand.Next(allowableChars.Length - 1))
			final.Append(allowableChars(rand.Next(0, allowableChars.Length)))
		Loop Until final.Length = lenStr
		Debug.WriteLine(final.Length)
		Return If(upper, final.ToString.ToUpper(), final.ToString)
	End Function



random.next has an inclusive lower bound and exclusive upper bound.

This post has been edited by dbasnett: 13 January 2009 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1