quantum-space-buddies/EpicOnlineTransport/RandomString.cs
2022-02-25 23:18:46 -08:00

42 lines
900 B
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Text;
public class RandomString
{
// Generates a random string with a given size.
public static string Generate(int size)
{
var builder = new StringBuilder(size);
var random = new Random();
// Unicode/ASCII Letters are divided into two blocks
// (Letters 6590 / 97122):
// The first group containing the uppercase letters and
// the second group containing the lowercase.
// char is a single Unicode character
var offsetLowerCase = 'a';
var offsetUpperCase = 'A';
const int lettersOffset = 26; // A...Z or a..z: length=26
for (var i = 0; i < size; i++)
{
char offset;
if (random.Next(0, 2) == 0)
{
offset = offsetLowerCase;
}
else
{
offset = offsetUpperCase;
}
var @char = (char)random.Next(offset, offset + lettersOffset);
builder.Append(@char);
}
return builder.ToString();
}
}