[英]How to generate Random 26 length character string with no duplicates(characters) in c#?
Random rnd = new Random();
string[] coupon = new string[26];
for (int i = 0; i < coupon.Length; i++)
{
coupon[i] = GenerateCoupon(26, rnd);
}
textBox1 .Text=(String.Join(Environment.NewLine, coupon));
***** Function***********\\ *****功能*********** \\
public static string GenerateCoupon(int length, Random random)
{
string characters = "abcdefghijklmnopqrstuvwxyz";
StringBuilder result = new StringBuilder(length);
{
result.Append(characters[random.Next(characters.Length)]);
return result.ToString();
}
Strings that code generates : 代码生成的字符串:
kheeuasampxqxmoohcrufznugp kheeuasampxqxmoohcrufznugp
vrlncvbftinynhdufjdikacjsi vrlncvbftinynhdufjdikacjsi
vblltkxeeapymbprtgaiojqkte vblltkxeeapymbprtgaiojqkte
qyfvpcvtazuiodbidcfgwcssgw qyfvpcvtazuiodbidcfgwcssgw
ijtlkbrpuyzilndsaqxlrxhggo ijtlkbrpuyzilndsaqxlrxhggo
emhngmostlapotqziciursddcc 紧急情况
vvflcnewwehgsntstrskbduroe vvflcnewwehgsntstrskbduroe
But i need a code that generates the string that have 26 character length and no duplicate character : 但是我需要一个代码来生成具有26个字符长度且没有重复字符的字符串:
abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba zyxwvutsrqponmlkjihgfedcba
mnbvcxzasdfghjklpoiuytrewq mnbvcxzasdfghjklpoiuytrewq
qazwsxedcrfvtgbyhnujmikolp qazwsxedcrfvtgbyhnujmikolp
bhuijnmkoplqazxswedcvfrtgb bhuijnmkoplqazxswedcvfrtgb
You can use Fisher-Yates shuffle , or simply 您可以使用Fisher-Yates shuffle ,也可以简单地使用
Random rnd = new Random();
var newstr = String.Concat("abcdefghijklmnopqrstuvwxyz".OrderBy(x => rnd.Next()));
查看一些随机播放算法,并将其应用于您的字符集。
You can just have a string of az
. 您只能输入一串
az
。 For example: "abcdefghijklmnopqrstuvwxyz". 例如:“ abcdefghijklmnopqrstuvwxyz”。
After that, randomly shuffle the string based on the index of each letter. 之后,根据每个字母的索引随机地对字符串进行随机排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.