1、生成字母和数字结合的随机序列:
Random rn =
new
Random();
string
charsToUse =
"AzByCxDwEvFuGtHsIrJqKpLoMnNmOlPkQjRiShTgUfVeWdXcYbZa1234567890"
;
MatchEvaluator RandomChar =
delegate
(Match m)
{
return
charsToUse[rn.Next( charsToUse.Length )].ToString();
};
Console.WriteLine( Regex.Replace(
"XXXX-XXXX-XXXX-XXXX-XXXX"
,
"X"
, RandomChar ) );
// Lv2U-jHsa-TUep-NqKa-jlBx
Console.WriteLine( Regex.Replace(
"XXXX"
,
"X"
, RandomChar ) );
// 8cPD
|
2、生成字节随机序列
byte[] randBytes = new byte[108];
Random randNum = new Random();
randNum.NextBytes(randBytes);