This article will show you how you can generate random
number in and random string in c#.net.
In this I will give example to generate random no, generate random no
below specific no, random no generation in given range, character generation,
password generation with combination of number and text using c#.net.
Random No: This will generate random no .
Random No: This will generate random no .
static void Main(string[] args)
{
Random r = new Random();
int randomno = r.Next();
Console.WriteLine(randomno);
}
|
Random no below
given no: This will generate random number on given below no.
static void Main(string[] args)
{
Random r = new Random();
int randomno = r.Next(20000);
Console.WriteLine(randomno);
}
|
As we run the above code will generate random no on below given no.
Range Random No: This will help us to generate random no as per give range.
static void Main(string[] args)
{
Random r = new Random();
int randomno = r.Next(1000,20000);
Console.WriteLine(randomno);
}
|
Above code will generate random no on given range only
Random Character: This piece of code will generate random character as per the given count
static void Main(string[] args)
{
int charCount = 5;
Random r = new Random();
StringBuilder str = new StringBuilder();
for (int i = 0; i < charCount; i++)
{
str.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * r.NextDouble()
+ 65))));
}
Console.WriteLine(str.ToString());
}
|
In above code below mention code is most important. This will help us to generate the random character.
Convert.ToChar(Convert.ToInt32(Math.Floor(26 * r.NextDouble()
+ 65)))
|
Output for above code is.
Thanks for reading the article please let me know you comment.YouTube Video:
0 comments:
Please let me know your view