Class tạo chuỗi (key) ngẫu nhiên
.NET Framework hổ trợ gần như hầu hết tất cả những gì LTV cần. Nhưng khi lập trình ứng dụng nếu bạn muốn tạo một chuổi ngẫu nhiên có độ dài theo ý muốn thì chưa có hàm nào hổ trợ. Sau đây là hàm bằng VB.NET giúp bạn trong việc này.
-
Option Strict On
-
Imports System.Text
-
'02082008164025
-
'class
-
'
-
'EQUIRES PROPERTIES: KeyLetters, KeyNumbers, MaxChars
-
'
-
-
Public Class RandomKeyGenerator
-
Dim Key_Letters As String
-
Dim Key_Numbers As String
-
Dim Key_Chars As Integer
-
Dim LettersArray As Char()
-
Dim NumbersArray As Char()
-
-
'02082008164025
-
'property
-
'
-
'WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()
-
'
-
-
Protected Friend WriteOnly Property KeyLetters() As String
-
Set(ByVal Value As String)
-
Key_Letters = Value
-
End Set
-
End Property
-
-
'02082008164025
-
'property
-
'
-
'WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()
-
'
-
-
Protected Friend WriteOnly Property KeyNumbers() As String
-
Set(ByVal Value As String)
-
Key_Numbers = Value
-
End Set
-
End Property
-
-
'02082008164025
-
'property
-
'
-
'WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()
-
'
-
-
Protected Friend WriteOnly Property KeyChars() As Integer
-
Set(ByVal Value As Integer)
-
Key_Chars = Value
-
End Set
-
End Property
-
-
'02082008072344
-
'function
-
'
-
'GENERATES A RANDOM STRING OF LETTERS AND NUMBERS.
-
'LETTERS CAN BE RANDOMLY CAPITAL OR SMALL.
-
'
-
'RETURNS THE
-
' RANDOMLY GENERATED KEY
-
-
Function Generate() As String
-
Dim i_key As Integer
-
Dim Random1 As Single
-
Dim arrIndex As Int16
-
Dim sb As New StringBuilder
-
Dim RandomLetter As String
-
-
'CONVERT LettersArray & NumbersArray TO CHARACTR ARRAYS
-
-
LettersArray = Key_Letters.ToCharArray
-
NumbersArray = Key_Numbers.ToCharArray
-
-
For i_key = 1 To Key_Chars
-
'START THE CLOCK
-
-
Randomize()
-
Random1 = Rnd()
-
arrIndex = -1
-
'IF THE VALUE IS AN EVEN NUMBER WE GENERATE A LETTER,
-
'OTHERWISE WE GENERATE A NUMBER
-
'THE NUMBER '111' WAS RANDOMLY CHOSEN. ANY NUMBER
-
'WILL DO, WE JUST NEED TO BRING THE VALUE
-
'ABOVE '0'
-
-
If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then
-
'GENERATE A RANDOM INDEX IN THE LETTERS
-
'CHARACTER ARRAY
-
-
Do While arrIndex < 0
-
arrIndex = _
-
Convert.ToInt16(LettersArray.GetUpperBound(0) _
-
* Random1)
-
Loop
-
RandomLetter = LettersArray(arrIndex)
-
'CREATE ANOTHER RANDOM NUMBER. IF IT IS ODD,
-
'WE CAPITALIZE THE LETTER
-
-
If (CType(arrIndex * Random1 * 99, Integer)) Mod 2 <> 0 Then
-
RandomLetter = LettersArray(arrIndex).ToString
-
RandomLetter = RandomLetter.ToUpper
-
End If
-
sb.Append(RandomLetter)
-
Else
-
'GENERATE A RANDOM INDEX IN THE NUMBERS
-
'CHARACTER ARRAY
-
-
Do While arrIndex < 0
-
arrIndex = _
-
Convert.ToInt16(NumbersArray.GetUpperBound(0) _
-
* Random1)
-
Loop
-
sb.Append(NumbersArray(arrIndex))
-
End If
-
Next
-
Return sb.ToString
-
End Function
-
-
End Class
Cách sử dụng:
-
Dim KeyGen As New RandomKeyGenerator
-
KeyGen.KeyChars = 10
-
KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz"
-
KeyGen.KeyNumbers = "0123456789"
-
MsgBox(KeyGen.Generate())
04 Tháng mười, 2008 lúc 9:05 sáng
:))
Save lại khi cần lượm ra sài hehe