Last night i decided to create a basic example on how to use the Random class in the .NET framework since randomization can also be useful in doing programs specially when designing applications that requires unique keys. One example of a program that requires a unique key is a random password generator.
I will not dive into details on how to create a random password generator, but we will take a closer look on how the Random class in .NET works.
As i have said awhile ago, i just show a basic example, not the complicated algorithm bloated with arrays and pointers.
Heres the C# sample:
using System;using System.Text;class RandomStringsAndNumbersCS{ /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { //Generate a random number between 1000 and 9999. int intRandomNumber = GenerateRandomInteger(1000, 9999); //Generate a random string with the size of 16 string strRandomString = GenerateRandomString(16); //print the result to the screen Console.WriteLine("Random Number:" + intRandomNumber.ToString()); Console.WriteLine("Random String:" + strRandomString); //Pause to view the result. Console.ReadLine(); } private static int GenerateRandomInteger(int intMin, int intMax) { //Create a new instance of the class Random Random randomNumber = new Random(); //Generate a random number using intMin as the minimum and intMax as the maximum return randomNumber.Next(intMin, intMax); } private static string GenerateRandomString(int intLenghtOfString) { //Create a new StrinBuilder that would hold the random string. StringBuilder randomString = new StringBuilder(); //Create a new instance of the class Random Random randomNumber = new Random(); //Create a variable to hold the generated charater. Char appendedChar; //Create a loop that would iterate from 0 to the specified value of intLenghtOfString for(int i= 0; i<= intLenghtOfString; ++i) { //Generate the char and assign it to appendedChar appendedChar = Convert.ToChar(Convert.ToInt32(26 * randomNumber.NextDouble()) + 65); //Append appendedChar to randomString randomString.Append(appendedChar); } //Convert randomString to String and return the result. return randomString.ToString(); }}
And here's the VB.NET sample:
Imports System.TextModule RandomStringsAndNumbersVB Sub Main() 'Generate a random number between 1000 and 9999. Dim intRandomNumber As Integer = GenerateRandomInteger(1000, 9999) 'Generate a random string with the size of 16 Dim strRandomString As String = GenerateRandomString(16) 'print the result to the screen Console.WriteLine("Random Number:" & intRandomNumber.ToString()) Console.WriteLine("Random String:" & strRandomString) 'Pause to view the result. Console.ReadLine() End Sub Private Function GenerateRandomInteger(ByVal intMin As Integer, ByVal intMax As Integer) As Integer 'Create a new instance of the class Random Dim randomNumber As Random = New Random 'Generate a random number using intMin as the minimum and intMax as the maximum Return randomNumber.Next(intMin, intMax) End Function Private Function GenerateRandomString(ByVal intLenghtOfString As Integer) As String 'Create a new StrinBuilder that would hold the random string. Dim randomString As StringBuilder = New StringBuilder 'Create a new instance of the class Random Dim randomNumber As Random = New Random 'Create a variable to hold the generated charater. Dim appendedChar As Char 'Create a loop that would iterate from 0 to the specified value of intLenghtOfString For i As Integer = 0 To intLenghtOfString 'Generate the char and assign it to appendedChar appendedChar = Convert.ToChar(Convert.ToInt32(26 * randomNumber.NextDouble()) + 65) 'Append appendedChar to randomString randomString.Append(appendedChar) Next 'Convert randomString to String and return the result. Return randomString.ToString() End FunctionEnd Module
Additional Material: If you are looking for advance ways to generate random strings and numbers take time to look at this links.
RS250/521 Algorithm and Mersenne Twister
and
How to Generate Strong Random Password
You can also download the source code of this tutorial for both C# and VB.NET.
Download the source code of this demo
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.