Last active
August 13, 2021 16:11
-
-
Save qluana7/c9cf8046dce1fcf6890f952c0572e30b to your computer and use it in GitHub Desktop.
랜덤 문자열을 얻을 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
[Flags] | |
public enum Options | |
{ | |
Upper = 1, | |
Lower = 1 << 1, | |
Number = 1 << 2 | |
} | |
public static string GetRandomString(int count, Options opt) | |
{ | |
var num = Enumerable.Range(48, 10); // 0 ~ 9 | |
var upp = Enumerable.Range(65, 26); // A ~ Z | |
var low = Enumerable.Range(97, 26); // a ~ z | |
char GetRandomChar() | |
{ | |
var tmp = Enumerable.Empty<int>(); | |
if (opt.HasFlag(Options.Upper)) | |
tmp = tmp.Concat(upp); | |
if (opt.HasFlag(Options.Lower)) | |
tmp = tmp.Concat(low); | |
if (opt.HasFlag(Options.Number)) | |
tmp = tmp.Concat(num); | |
if (!tmp.Any()) | |
throw new System.InvalidOperationException(); | |
return (char)tmp.ElementAt(new Random().Next(tmp.Count())); | |
} | |
string result = string.Empty; | |
for (int i = 0; i < count; i++) | |
result += GetRandomChar(); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment