C#随机生成n位数字和字母的方法
2008-05-07 16:19
891 查看
C#随机生成n位数字和字母的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class GetRandom
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static int roCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static char[] constant =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'2','3','4','5','6','7','8','9',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 由下面所列的字符抽取,如果为了避免出现一些容易混淆的字符,上面的已经筛选
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// '0','1','2','3','4','5','6','7','8','9',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public enum RandomType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
All,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Number,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Uppercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Lowercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NumberAndUppercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NumberAndLowercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UppercasedAndLowercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static string GenerateRandom(int Length,RandomType rt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int initsize=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int beginsize = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int endsize=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Boolean IsCross=false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch (rt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.All:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = constant.Length; //constant数组的最大个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 1; //constant数组的开始下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = constant.Length; //constant数组的结束下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.Lowercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 24; //少了2个小写L,0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 9;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = 32;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.Uppercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 25; //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 33;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = constant.Length; //constant数组的结束下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.Number:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = 8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.UppercasedAndLowercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = constant.Length-8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 9;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = constant.Length; //constant数组的结束下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.NumberAndLowercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 32;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = 32;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.NumberAndUppercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IsCross = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(initsize);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Random rd = new Random(unchecked(roCount * (int)DateTime.Now.Ticks));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
roCount++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!IsCross)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newRandom.Append(constant[rd.Next(beginsize, endsize)]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newRandom.Append(constant[rd.Next(1, 8)]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newRandom.Append(constant[rd.Next(33, constant.Length)]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return newRandom.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class GetRandom
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static int roCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static char[] constant =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'2','3','4','5','6','7','8','9',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 由下面所列的字符抽取,如果为了避免出现一些容易混淆的字符,上面的已经筛选
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// '0','1','2','3','4','5','6','7','8','9',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public enum RandomType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
All,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Number,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Uppercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Lowercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NumberAndUppercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NumberAndLowercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UppercasedAndLowercased,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static string GenerateRandom(int Length,RandomType rt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int initsize=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int beginsize = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int endsize=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Boolean IsCross=false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch (rt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.All:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = constant.Length; //constant数组的最大个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 1; //constant数组的开始下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = constant.Length; //constant数组的结束下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.Lowercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 24; //少了2个小写L,0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 9;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = 32;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.Uppercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 25; //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 33;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = constant.Length; //constant数组的结束下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.Number:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = 8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.UppercasedAndLowercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = constant.Length-8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 9;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = constant.Length; //constant数组的结束下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.NumberAndLowercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
initsize = 32;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginsize = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endsize = 32;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//IsCross = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case RandomType.NumberAndUppercased:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IsCross = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(initsize);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Random rd = new Random(unchecked(roCount * (int)DateTime.Now.Ticks));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
roCount++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!IsCross)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newRandom.Append(constant[rd.Next(beginsize, endsize)]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newRandom.Append(constant[rd.Next(1, 8)]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newRandom.Append(constant[rd.Next(33, constant.Length)]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return newRandom.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- [C#]生成一串包含字符和数字的随机字符串的方法
- 随机生成字母加数字16位数方法
- C#中随机生成一串字母+数字的字符串
- c++中生成n个m位的随机不重复字符串的一种方法(字母+数字)
- C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密
- C# .NET Random 生成随机数字和字母
- C# .NET Random 生成随机数字和字母
- C#各种数字字母指定长度随机方法
- C#生成字母和数字组成的随机文件
- C# 生成验证码取随机数字加字母(改进版)
- C# 生成验证码,取随机数字加字母
- [C#]生成一串包含字符和数字的随机字符串的方法
- C# 生成验证码,取随机数字加字母
- C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密
- C#随机生成数字、字母
- C# 生成验证码取随机数字加字母(改进版)
- [C#]生成一串包含字符和数字的随机字符串的方法
- C# Random生成随机数字和字母
- php随机生成数字字母组合的方法