How do I generate a random string?

bunze

Well-known member
Joined
Aug 4, 2005
Messages
52
Location
Adirolf
Programming Experience
1-3
say i want to generate a string like IO45NB210AFS3 or something, how would that be done?
 
Here's Your Solution

I took it from google....its a nice code

Module1.vb

VB.NET:
Module[/color][/size][size=2] Module1
[/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Main()
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] KeyGen [/size][size=2][color=#0000ff]As[/color][/size][size=2] RandomKeyGenerator
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] NumKeys [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i_Keys [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] RandomKey [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#008000]' MODIFY THIS TO GET MORE KEYS [/color][/size]
[size=2]NumKeys = 20
KeyGen = [/size][size=2][color=#0000ff]New[/color][/size][size=2] RandomKeyGenerator
 
KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz"
KeyGen.KeyNumbers = "0123456789"
KeyGen.KeyChars = 12

[/size][size=2][color=#0000ff]For[/color][/size][size=2] i_Keys = 1 [/size][size=2][color=#0000ff]To[/color][/size][size=2] NumKeys
    RandomKey = KeyGen.Generate()
    Console.WriteLine(RandomKey) 
[/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2]Console.WriteLine("Press any key to exit...")
 
Console.Read()
 
[/size][size=2][color=#0000ff]End Sub

End Module[/color][/size]


RandomKeyGenerator.vb
VB.NET:
[size=2][color=#0000ff][size=2][color=#0000ff]Option[/color][/size][size=2][color=#0000ff]Strict[/color][/size][size=2][color=#0000ff]On
 
Imports[/color][/size][size=2][color=#000000] System.Text[/color]
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2][color=#000000] RandomKeyGenerator[/color]
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Key_Letters [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Key_Numbers [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Key_Chars [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] LettersArray [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Char[/color][/size][size=2]()
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] NumbersArray [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Char[/color][/size][size=2]()
 
[/size][size=2][color=#0000ff]Protected[/color][/size][size=2][color=#0000ff]Friend[/color][/size][size=2][color=#0000ff]WriteOnly[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] KeyLetters() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2])
 
Key_Letters = Value
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff] Set
 
[/color][/size][size=2][color=#0000ff]End Property
 
[/color][/size][size=2][color=#0000ff]Protected[/color][/size][size=2][color=#0000ff] Friend[/color][/size][size=2][color=#0000ff] WriteOnly[/color][/size][size=2][color=#0000ff] Property[/color][/size][size=2] KeyNumbers() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] String
 
[/color][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2])
 
Key_Numbers = Value
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff] Set
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff] Property
 
[/color][/size][size=2][color=#0000ff]Protected[/color][/size][size=2][color=#0000ff] Friend[/color][/size][size=2][color=#0000ff] WriteOnly[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] KeyChars() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] Integer
 
[/color][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
 
Key_Chars = Value
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Set
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff] Property
 
[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] Generate() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i_key [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] Integer
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Random1 [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] Single
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] arrIndex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int16
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sb [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] StringBuilder
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] RandomLetter [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] String
 
[/color][/size][size=2][color=#008000]' CONVERT LettersArray & NumbersArray TO CHARACTR ARRAYS
 
[/color][/size][size=2]LettersArray = Key_Letters.ToCharArray
 
NumbersArray = Key_Numbers.ToCharArray
 
[/size][size=2][color=#0000ff]For[/color][/size][size=2] i_key = 1 [/size][size=2][color=#0000ff]To[/color][/size][size=2] Key_Chars
 
[/size][size=2][color=#008000]'START THE CLOCK [/color][/size]
[size=2]Randomize()
Random1 = Rnd()
 
arrIndex = -1
 
[/size][size=2][color=#008000]'IF THE VALUE IS AN EVEN NUMBER WE GENERATE A LETTER, OTHERWISE WE 'GENERATE A NUMBER [/color][/size]

[size=2][color=#008000]'THE NUMBER '111' WAS RANDOMLY CHOSEN. ANY NUMBER WILL DO, WE JUST 'NEED TO BRING THE VALUE ABOVE '0' [/color][/size]

[size=2][color=#0000ff]If[/color][/size][size=2] ([/size][size=2][color=#0000ff]CType[/color][/size][size=2](Random1 * 111, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2])) [/size][size=2][color=#0000ff]Mod[/color][/size][size=2] 2 = 0 [/size][size=2][color=#0000ff]Then
[/color][/size]
[size=2][color=#008000]'GENERATE A RANDOM LOCATION IN THE LETTERS CHARACTER ARRAY [/color][/size]
 
[size=2][color=#0000ff]Do[/color][/size][size=2][color=#0000ff]While[/color][/size][size=2] arrIndex < 0
arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * Random1)
 
[/size][size=2][color=#0000ff]Loop
 
[/color][/size][size=2]RandomLetter = LettersArray(arrIndex)
 
[/size][size=2][color=#008000]'CREATE ANOTHER RANDOM NUMBER. IF IT IS ODD, WE CAPITALIZE THE 'LETTER [/color][/size]
[size=2][color=#0000ff]If[/color][/size][size=2] ([/size][size=2][color=#0000ff]CType[/color][/size][size=2](arrIndex * Random1 * 99, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2])) [/size][size=2][color=#0000ff]Mod[/color][/size][size=2] 2 <> 0 [/size][size=2][color=#0000ff]Then
[/color][/size][size=2]RandomLetter = LettersArray(arrIndex).ToString
 
RandomLetter = RandomLetter.ToUpper
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff] If
 
[/color][/size][size=2]sb.Append(RandomLetter)
 
[/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2][color=#008000]'GENERATE A RANDOM LOCATION IN THE NUMBERS CHARACTER ARRAY 
 
[/color][/size][size=2][color=#0000ff]Do[/color][/size][size=2][color=#0000ff]While[/color][/size][size=2] arrIndex < 0
 
arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1)
 
[/size][size=2][color=#0000ff]Loop
 
[/color][/size][size=2]sb.Append(NumbersArray(arrIndex))
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff] If
 
[/color][/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] sb.ToString
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff] Function
 
End[/color][/size][size=2][color=#0000ff] Class
 
[/color][/size][/color][/size][size=2][color=#0000ff]
[/color][/size]
 
Last edited by a moderator:
I made it myself...........You can use it for FREE!!!!!!!!!!!

Public Class Security

Public Enum PatternType

UpperCaseOnly

SmallCaseOnly

NumbersOnly

SmallAndNumbers

UpperAndNumbers

End Enum

' to return a said number of string...

Public Shared Function GetString(ByVal total As Integer, ByVal pattern As PatternType) As String

Randomize()

If (total = 0) Then

Return "InvalidNumberOfLength"

End If

' return type

Dim rString As String = ""

Dim rand As New Random

If (pattern = PatternType.NumbersOnly) Then

Dim i As Integer

Dim random As Integer

For i = 0 To total - 1

random =
CInt(Int((9 * Rnd()) + 0))

rString &= random

Next

ElseIf (pattern = PatternType.SmallAndNumbers) Then

Dim i As Integer

For i = 0 To total - 1

' generate a number between 1 and 2 and then if it is 1

' then generate a Small Alphaber and if it is 2 then generate a

' number

Dim numb As Integer = CInt(Int((2 * Rnd()) + 1))

Dim random As Integer

If (numb = 1) Then

random = CInt(Int((122 - 97 + 1) * Rnd() + 97))

' convert it to small alphabet character and then add

rString &= Chr(random)

Else

random = CInt(Int((9 * Rnd()) + 0))

rString &= random

End If

Next

ElseIf (pattern = PatternType.SmallCaseOnly) Then

Dim i As Integer

For i = 0 To total - 1

Dim random As Integer

random = CInt(Int((122 - 97 + 1) * Rnd() + 97))

' convert it to small alphabet character and then add

rString &= Chr(random)

Next

ElseIf (pattern = PatternType.UpperAndNumbers) Then

Dim i As Integer

For i = 0 To total - 1

' generate a number between 1 and 2 and then if it is 1

' then generate a Upper Alphabet and if it is 2 then generate a

' number

Dim numb As Integer = CInt(Int((2 * Rnd()) + 1))

Dim random As Integer

If (numb = 1) Then

random = CInt(Int((90 - 65 + 1) * Rnd() + 65))

' convert it to upper alphabet character and then add

rString &= Chr(random)

Else

random = CInt(Int((9 * Rnd()) + 0))

rString &= random

End If

Next

ElseIf (pattern = PatternType.UpperCaseOnly) Then

Dim i As Integer

For i = 0 To total - 1

Dim random As Integer

random = CInt(Int((90 - 65 + 1) * Rnd() + 65))

' convert it to upper alphabet character and then add

rString &= Chr(random)

Next

End If

' return generated...

Return rString

End Function

End Class


Just make a class named 'Security' in your project and copy and paste the code and there is a method named GetString containing many things and pass the appropriate parameter and get the required result.

Regards
 
here's a basic one that's simple and gets the job done:
VB.NET:
	Friend Function RandomString(ByVal MaxLength As Integer) As String
		Dim strRandomString As String
		Dim RndNum As New Random
		Dim intCounter As Integer
		For intCounter = 1 To MaxLength
			Select Case RndNum.Next(0, 35)
				Case 0 : strRandomString &= "A"
				Case 1 : strRandomString &= "B"
				Case 2 : strRandomString &= "C"
				Case 3 : strRandomString &= "D"
				Case 4 : strRandomString &= "E"
				Case 5 : strRandomString &= "F"
				Case 6 : strRandomString &= "G"
				Case 7 : strRandomString &= "H"
				Case 8 : strRandomString &= "I"
				Case 9 : strRandomString &= "J"
				Case 10 : strRandomString &= "K"
				Case 11 : strRandomString &= "L"
				Case 12 : strRandomString &= "M"
				Case 13 : strRandomString &= "N"
				Case 14 : strRandomString &= "O"
				Case 15 : strRandomString &= "P"
				Case 16 : strRandomString &= "Q"
				Case 17 : strRandomString &= "R"
				Case 18 : strRandomString &= "S"
				Case 19 : strRandomString &= "T"
				Case 20 : strRandomString &= "U"
				Case 21 : strRandomString &= "V"
				Case 22 : strRandomString &= "W"
				Case 23 : strRandomString &= "X"
				Case 24 : strRandomString &= "Y"
				Case 25 : strRandomString &= "Z"
				Case 26 : strRandomString &= "0"
				Case 27 : strRandomString &= "1"
				Case 28 : strRandomString &= "2"
				Case 29 : strRandomString &= "3"
				Case 30 : strRandomString &= "4"
				Case 31 : strRandomString &= "5"
				Case 32 : strRandomString &= "6"
				Case 33 : strRandomString &= "7"
				Case 34 : strRandomString &= "8"
				Case 35 : strRandomString &= "9"
			End Select
		Next intCounter
		Return strRandomString
	End Function

feel free to use it/modify it
 
Back
Top