connectseekers.com

Saturday, 21 March 2020

Random Number and String Generation in C#

3/21/2020 - By Pranav Singh 0


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 .


static void Main(string[] args)
        {
            Random r = new Random();
            int randomno = r.Next();
            Console.WriteLine(randomno);
        }

Below is the output of the above code.


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:



Tags: ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top