Jump to content

cbox


[VB]How do I generate random numbers and letters?


Recommended Posts

Posted
Module ye

  Sub Main()
     Dim intRandomNumber As Integer = GenerateRandomInteger(1000, 9999)
     Dim strRandomString As String = GenerateRandomString(16)
     Console.WriteLine("Random Number:" & intRandomNumber.ToString())
     Console.WriteLine("Random String:" & strRandomString)
     Console.ReadLine()
   End Sub

  Private Function GenerateRandomInteger(ByVal intMin As Integer, ByVal intMax As Integer) As Integer
     Dim randomNumber As Random = New Random
     Return randomNumber.Next(intMin, intMax)
  End Function

  Private Function GenerateRandomString(ByVal intLenghtOfString As Integer) As String
     Dim randomString As StringBuilder = New StringBuilder
     Dim randomNumber As Random = New Random
     Dim appendedChar As Char
     For i As Integer = 0 To intLenghtOfString
        appendedChar = Convert.ToChar(Convert.ToInt32(26 * randomNumber.NextDouble()) + 65)
        randomString.Append(appendedChar)
     Next
     Return randomString.ToString()
  End Function

End Module

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...