C#生成随机序列


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);


posted @ 2012-01-29 23:30  陈哈哈  阅读(949)  评论(0编辑  收藏  举报