Random number and string:private static string RandomNumberGenerator(int length){System.Security.Cryptography.RandomNumberGenerator rng =System.Security.Cryptography.RandomNumberGenerator.Create();char[] chars = new char[length];//based on your requirment you can take only alphabets or numberstring validChars ="abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ1234567890";for (int i=0; i< length; i++){byte[] bytes = new byte[1];rng.GetBytes(bytes);Random rnd = new Random(bytes[0]);chars[i] = validChars[rnd.Next(0,61)];}return (new string(chars));}
The Random class defined in the .NET Framework class library provides functionality togenerate random numbers.The Random class constructors have two overloaded forms. It takes either no value or ittakes a seed value.The Random class has three public methods - Next, NextBytes, and NextDouble. The Nextmethod returns a random number, NextBytes returns an array of bytes filled with randomnumbers, and NextDouble returns a random number between 0.0 and 1.0. The Next methodhas three overloaded forms and allows you to set the minimum and maximum range of therandom number.The following code returns a random number:intnum = random.Next();The following code returns a random number less than 1000.
Leave a Comment