How to generate a string randomly using C#?

Generating random strings in C# is useful for creating passwords, test data, or unique identifiers. There are several approaches to generate random strings, from simple character-based methods to more advanced techniques using predefined character sets.

Syntax

Following is the basic syntax for generating random strings using Random class −

Random random = new Random();
char randomChar = (char)random.Next(startValue, endValue);

Following is the syntax using a character array −

char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
char randomChar = chars[random.Next(chars.Length)];

Using ASCII Values for Random String Generation

This method generates random characters by converting ASCII values to characters. ASCII values 65-90 represent uppercase letters A-Z −

using System;
using System.Text;

class Program {
   static void Main() {
      StringBuilder str = new StringBuilder();
      char c;
      Random random = new Random();
      
      for (int i = 0; i < 6; i++) {
         c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
         str.Append(c);
      }
      
      Console.WriteLine("Random string: " + str.ToString());
   }
}

The output of the above code is −

Random string: QMKFBT

Using Character Array for Mixed Case Strings

This approach uses a predefined character set for more control over the allowed characters −

using System;
using System.Text;

class Program {
   static void Main() {
      string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      StringBuilder result = new StringBuilder();
      Random random = new Random();
      
      for (int i = 0; i < 8; i++) {
         result.Append(chars[random.Next(chars.Length)]);
      }
      
      Console.WriteLine("Random alphanumeric string: " + result.ToString());
   }
}

The output of the above code is −

Random alphanumeric string: K3mN9pQr

Using GUID for Unique Random Strings

For truly unique strings, you can use Guid.NewGuid() which generates a globally unique identifier −

using System;

class Program {
   static void Main() {
      string randomGuid = Guid.NewGuid().ToString();
      Console.WriteLine("GUID string: " + randomGuid);
      
      string shortGuid = Guid.NewGuid().ToString("N").Substring(0, 8);
      Console.WriteLine("Short GUID: " + shortGuid);
   }
}

The output of the above code is −

GUID string: 6f8ac3e4-9b2d-4f5a-8c1e-7d3b9a8e4f2c
Short GUID: a7b3c4d5

Comparison of Methods

Method Best For Character Set
ASCII Values Simple uppercase letters A-Z only
Character Array Custom character sets Any characters you define
GUID Unique identifiers Hexadecimal (0-9, A-F)

Conclusion

C# provides multiple ways to generate random strings: ASCII-based methods for simple character ranges, character arrays for custom sets, and GUIDs for guaranteed uniqueness. Choose the method based on your specific requirements for character variety and string length.

Updated on: 2026-03-17T07:04:35+05:30

709 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements