Library code snippets
Randomizing Numbers And Letters
Code
I found this and edited it a little to meet my requirments
I put this in a sperate .vb file that is in my project that way it can be accessed from different screens and it is easier to understand.
Option Strict On
Imports System.Text
Imports System.Threading
''' REQUIRES PROPERTIES: KeyLetters, KeyNumbers, MaxChars
Public Class RandomKeyGenerator
Dim Key_Letters As String
Dim Key_Numbers As String
Dim Key_Chars As Integer
Dim WType As String
Dim LettersArray As Char()
Dim NumbersArray As Char()
Dim i_key As Integer
Dim Random1 As Single
Dim Random2 As Single
Dim Random3 As Single
Dim Num As Single
Dim arrIndex As Int16
Dim sb As New StringBuilder()
Dim RandomLetter As String
''' Here are all the factors that are sent from the calling program
Protected Friend WriteOnly Property KeyLetters() As String
Set(ByVal Value As String)
Key_Letters = Value
End Set
End Property
Protected Friend WriteOnly Property KeyNumbers() As String
Set(ByVal Value As String)
Key_Numbers = Value
End Set
End Property
Protected Friend WriteOnly Property KeyChars() As Integer
Set(ByVal Value As Integer)
Key_Chars = Value
End Set
End Property
Protected Friend WriteOnly Property Type() As String
Set(ByVal Value As String)
WType = Value
End Set
End Property
''' GENERATES A RANDOM STRING OF LETTERS AND NUMBERS.
''' LETTERS CAN BE RANDOMLY CAPITAL OR SMALL.
''' <returns type="String">
''' RETURNS THE RANDOMLY GENERATED KEY
Function Generate() As String
''' CONVERT LettersArray & NumbersArray TO CHARACTR ARRAYS
If WType = "L" Then
LettersArray = Key_Letters.ToCharArray
Else
NumbersArray = Key_Numbers.ToCharArray
End If
For i_key = 1 To Key_Chars
''' START THE CLOCK
Randomize()
Random1 = Rnd()
Thread.Sleep(10)
Randomize()
Random2 = Rnd()
arrIndex = -1
''' IF THE VALUE IS L GENERATE A LETTER,
''' OTHERWISE WE GENERATE A NUMBER
If WType = "L" Then
''' GENERATE A RANDOM INDEX IN THE LETTERS
''' CHARACTER ARRAY
Do While arrIndex < 0
OddEven()
If Num Mod 2 = 1 Then
arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * Random1)
Else
arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * Random2)
End If
Loop
RandomLetter = LettersArray(arrIndex)
sb.Append(RandomLetter)
Else
''' GENERATE A RANDOM INDEX IN THE NUMBERS
''' CHARACTER ARRAY
Do While arrIndex < 0
oddEven()
If Num Mod 2 = 1 Then
arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1)
Else
arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random2)
End If
Loop
sb.Append(NumbersArray(arrIndex))
End If
Next
Return sb.ToString
End Function
'I added this bit of code to make it just a little bit more random in the chossing 'of the letters or numbers it is called from above and just gets a radom 'number and it will divide by 2 if it is even there will be no remiander else ther 'will be a remainder.
Private Sub OddEven()
Randomize()
Random3 = Rnd()
Num = Random3 / 2
End Sub
End Class
Here is the calling code to Get the number or letters. The intenger after the PinNumbers sets how many characters I require back from the program.
Private Sub GetPin()
Pin = LCase(dtPin.Rows(0).Item("Init"))
PinNumbers(3)
Pin += RandomKey
PinLetters(1)
Pin += RandomKey
PinNumbers(2)
Pin += RandomKey
End Sub
From the calling code it brings in the number of characters I require and it is passesed by the KeyChars. The Types tells the program wether it is numbers or letters.
Private Sub PinNumbers(ByVal Number As Integer)
NumKeys = 1
KeyGen = New RandomKeyGenerator()
KeyGen.KeyNumbers = "23456789"
KeyGen.KeyChars = Number
KeyGen.Type = "N"
For i_Keys = 1 To NumKeys
RandomKey = KeyGen.Generate()
Next
End Sub
Private Sub PinLetters(ByVal Number)
NumKeys = 1
KeyGen = New RandomKeyGenerator()
KeyGen.KeyLetters = "abcdefghijkmnpqrstuvwxyz"
KeyGen.KeyChars = Number
KeyGen.Type = "L"
For i_Keys = 1 To NumKeys
RandomKey = KeyGen.Generate()
Next
End Sub
'As you can see you can pick and choose letters and numbers. I chose to not 'do 1 and 0 and I and O for the fact that they are easily mistakable for 'eachother. I use this to automatically come up for a password for our 'employess. It uses the intials for the first two letters after that it is all the code that does it.
Related articles
Related discussion
-
Sharepoint : GroupBy results according to custom property
by sampadasanjay (0 replies)
-
i have struck with my project in vb.net
by jetski (4 replies)
-
Microsoft MVP Donald Belcham to Speak on Visual Studio, C# vNext, Aspect Oriented Programming, Live Mesh
by Shaggy123 (0 replies)
-
Error in VB code
by glib162002 (0 replies)
-
Very slow inserts using SqlCommand.ExecuteNonQuery()
by porchelvi (1 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
This thread is for discussions of Randomizing Numbers And Letters.