2019年5月23日 星期四

How can I generate random alphanumeric strings?

private static Random random = new Random();
public static string RandomString(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}

from : https://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings

1 則留言:

  1. const string src = "abcdefghijklmnopqrstuvwxyz0123456789";
    int length = 16;
    var sb = new StringBuilder();
    Random RNG = new Random();
    for (var i = 0; i < length; i++)
    {
    var c = src[RNG.Next(0, src.Length)];
    sb.Append(c);
    }
    MessageBox.Show(sb.ToString());

    More examples on...C# Random string

    回覆刪除