How can I generate random alphanumeric strings?

后端 未结 30 2777
予麋鹿
予麋鹿 2020-11-22 03:17

How can I generate a random 8 character alphanumeric string in C#?

30条回答
  •  醉话见心
    2020-11-22 03:58

    A solution without using Random :

    var chars = Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 8);
    
    var randomStr = new string(chars.SelectMany(str => str)
                                    .OrderBy(c => Guid.NewGuid())
                                    .Take(8).ToArray());
    

提交回复
热议问题