ARTICLE

Generating Random Number and String in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 November 19, 2004
The Random class defined in the .NET Framework class library provides functionality to generate random numbers. This article shows you how you can use this class to generate random numbers and strings and even combination of both.
 
Reader Level:

The Random class defined in the .NET Framework class library provides functionality to generate random numbers.

The Random class constructors have two overloaded forms. It takes either no value or it takes a seed value.

The Random class has three public methods-Next, NextBytes, and NextDouble. The Next method returns a random number, NextBytes returns an array of bytes filled with random numbers, and NextDouble returns a random number between 0.0 and 1.0. The Next method has three overloaded forms and allows you to set the minimum and maximum range of the random number.

The following code returns a random number:

Dim num As Integer = random.Next()

The following code returns a random number less than 1000.

Dim num As Integer = random.Next(1000)

The following code returns a random number between min and max:

Private Function RandomNumber(min As Integer, max As Integer) As Integer
   Dim random As New Random()
   Return random.Next(min, max)
End Function 'RandomNumber

At some point, you may also want to generate random strings. I have created a method, which takes first parameter as the size of string and second parameter if you want the string to be lowercase.

'/ '/ Generates a random string with the given length
'/ '/ Size of the string '/ If true, generate lowercase string
'/ Random string
Private Function RandomString(size As Integer, lowerCase As Boolean) As String
   Dim builder As New StringBuilder()
   Dim random As New Random()
   Dim ch As Char
   Dim i As Integer
   For i = 0 To size - 1
      ch = Convert.ToChar(Convert.ToInt32((26 * random.NextDouble() + 65)))
      builder.Append(ch)
   Next
   i
If lowerCase Then
      Return builder.ToString().ToLower()
   End If
   Return builder.ToString()
End Function 'RandomString

You can even combine the two methods-RandomNumber and RandomString to generate a combination of random string and numbers. For example, the following code generates a password of length 10 with first 4 letters lowercase, next 4 letters numbers, and last 2 letters as uppercase.

Public Function GetPassword() As String
   Dim builder As New StringBuilder()
   builder.Append(RandomString(4,
True))
   builder.Append(RandomInt(1000, 9999))
   builder.Append(RandomString(2,
False))
   Return builder.ToString()
End Function 'GetPassword

share this article :
post comment
 

Hi Please help me how can i write a program that generate password that does not contain &(()$#@?><}{": or something else Thanks

Posted by mohammad dehghani Jul 14, 2011

how can i generate random numbers following certain distribution in visual basic 2008.please help me..... thanks in advance....

Posted by anamika saxena Mar 23, 2011

i need to create a number using the computers date/time (to the second) So it can be used as a security number, how can this happen?

Posted by Amanda Ruddock Mar 21, 2010

Thanks! This is what I needed! : )

Posted by Stephen Dmytriw Nov 17, 2009

How can one create a random number of ten numbers with the first three number similar?

Posted by Bongekile Mshweshwe Sep 26, 2009
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor