- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to generate a string randomly using C#?
Firstly, set a string.
StringBuilder str = new StringBuilder();
Use Random.
Random random = new Random((int)DateTime.Now.Ticks);
Now loop through a number which is the length of the random string you want.
for (int i = 0; i < 4; i++) { c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))); str.Append(c); }
On every iteration above, a random character is generated and appended to form a string.
The following is the complete example −
Example
using System.Text; using System; class Program { static void Main() { StringBuilder str = new StringBuilder(); char c; Random random = new Random((int)DateTime.Now.Ticks); for (int i = 0; i < 4; i++) { c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))); str.Append(c); } Console.WriteLine(str.ToString()); } }
Output
ATTS
- Related Articles
- How to generate a string of bits using Python?
- How to randomly select an item from a string in Python?
- How to generate a repeated values vector with each value in output selected randomly in R?
- How to reverse a String using C#?
- How to generate a 24bit hash using Python?
- How to generate a strong password using PowerShell?
- How to generate the first 100 Odd Numbers using C#?
- How to generate the first 100 even Numbers using C#?
- How to generate XML using Python?
- How to generate pascals triangle for the given number using C#?
- How to generate a random number in C++?
- How to generate prime twins using Python?
- How to generate JSON output using Python?
- How to generate statistical graphs using Python?
- How to generate prime numbers using Python?

Advertisements