Jump to content

  •  

  • iBotModz CBox


    Photo

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


    • Please log in to reply
    3 replies to this topic

    #1 littlechikon

    littlechikon

      Private

    • Members+

    • 36 posts
    • Joined: 16-July 08

    Posted 09 July 2009 - 02:53 PM

    I need to make a program that generates 5 random numbers and letters, in VB9 but I am having trouble. Can someone help me ? =]

    #2 Rogue Modder

    Rogue Modder

      Class of 2008

    • VIP

    • 1,328 posts
    • Joined: 02-January 09
    • Gender:Male
    • Location:London, UK

    Posted 09 July 2009 - 02:58 PM

    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


    #3 littlechikon

    littlechikon

      Private

    • Members+

    • 36 posts
    • Joined: 16-July 08

    Posted 09 July 2009 - 03:04 PM

    =O thanks

    #4 ixGAMEOVERxixx

    ixGAMEOVERxixx

      Private Grade 2

    • VIP

    • 478 posts
    • Joined: 27-October 08
    • Gender:Not Telling
    • Location:Custom Games

    Posted 10 July 2009 - 12:42 PM

    Or..

    Dim Random As New Random()
       TextBox1.Text = Chr(Random.Next(65, 90)) & Chr(Random.Next(65, 90)) & Chr(Random.Next(65, 90)) & Chr(Random.Next(65, 90))

    :) Life is simple