笨方法 Random ra =new Random(); string pt=""; int res=0; switch (pt) { case "3": res= ra.Next(100, 999); break; case "5": res= ra.Next(10000, 99999); break; ..... }
// 生成随机数字 // <param name="length">生成长度</param> public static string Number(int Length) { return Number(Length, false); } /// 生成随机数字 /// <param name="Length">生成长度</param> /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param> public static string Number(int Length,bool Sleep) { if(Sleep) System.Threading.Thread.Sleep(3); string result = ""; System.Random random = new Random(); for (int i = 0; i < Length; i++) { result += random.Next(10).ToString(); } return result; } /// <summary> /// 生成随机纯字母随机数 /// </summary> /// <param name="IntStr">生成长度</param> /// <returns></returns> public static string Str_char(int Length) { return Str_char(Length, false); } /// <summary> /// 生成随机纯字母随机数 /// </summary> /// <param name="Length">生成长度</param> /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param> /// <returns></returns> public static string Str_char(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(3); char[] Pattern = new char[] { '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' }; string result = ""; int n = Pattern.Length; System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) { int rnd = random.Next(0, n); result += Pattern[rnd]; } return result; } }
字母随机或者:/// <summary> /// 字母随机数 /// </summary> /// <param name="n">生成长度</param> /// <returns></returns> public static string RandLetter(int n) { char[] arrChar = new char[]{ 'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x', '_', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z' }; StringBuilder num = new StringBuilder(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < n; i++) { num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); } return num.ToString(); }
字母随机或者:/// <summary> /// 字母随机数 /// </summary> /// <param name="n">生成长度</param> /// <returns></returns> public static string RandLetter(int n) { char[] arrChar = new char[]{ 'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x', '_', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z' }; StringBuilder num = new StringBuilder(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < n; i++) { num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); } return num.ToString(); }
string[] s = new string[26]{"a","b","c","d","e","f","g",...}; Random ran = new Random(); int n = 0; int m=0; string nValue = ""; string mValue = ""; int index = 0; for(int i = 0;i<n;i++) { index = ran.Next(0,25); nValue += s[index]; } for(int i=0;i<m;i++) { index = ran.Next(0,9); mValue += index.ToString(); }
StringBuilder sb = new StringBuilder(); string Table = "abcdefghijklmnopqrstuvwxyz"; int[] p = new int[26];//字符索引 int Length = 3 - 1;//字符串的长度 int i = Length; p[i] = -1; int a = 0; while (a++ <= Length) sb.Append("a"); while (i > -1) { if (p[i] == 25) { sb.Remove(i, 1); i--; continue; } else { p[i] = p[i] + 1; sb[i] = Table[p[i]]; if (i < Length) { i++; p[i] = -1; sb.Append(" "); continue; } } Response.Write(sb.ToString() + "<br/>"); } 或者把值存入ISET里来查
/// <summary> /// 生成随机字符串 /// </summary> /// <param name="length">目标字符串的长度</param> /// <param name="useNum">是否包含数字,1=包含,默认为包含</param> /// <param name="useLow">是否包含小写字母,1=包含,默认为包含</param> /// <param name="useUpp">是否包含大写字母,1=包含,默认为包含</param> /// <param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param> /// <param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param> /// <returns>指定长度的随机字符串</returns> public string GetRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom) { byte[] b = new byte[4]; new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); Random r = new Random(BitConverter.ToInt32(b, 0)); string s = null, str = custom; if (useNum == true) { str += "0123456789"; } if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; } if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; } for (int i = 0; i < length; i++) { s += str.Substring(r.Next(0, str.Length - 1), 1); } return s; }
<script type="text/javascript" language="javascript">function CreateStr() { var j=document.getElementById("input2").value; if(isNaN(j)) { alert("请输入整数"); } var tmpCh = ""; var i=1; for(i=1;i<=j;i++) { tmpCh += String.fromCharCode(Math.floor( Math.random() * 26) + "a".charCodeAt(0)); } alert(tmpCh); } function CreateNo() { var j=document.getElementById("input3").value; if(isNaN(j)) { alert("请输入整数"); }var s = (Math.floor(Math.random()*10000) % j+1).toString(); for(var i=1;i<j; i++) { s = s + (Math.floor(Math.random()*10000) % 10).toString(); } alert(s); } </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE></HEAD><BODY> <input type="input" id="input2" /> <input type="button" onclick="CreateStr()" value="生成随机字母" width="120px" height="28px"/> <input type="input" id="input3" /> <input type="button" onclick="CreateNo()" value="生成随机数" width="120px" height="28px"/> </BODY> </HTML>
private static StringBuilder getRandom( bool isOverlap, int number, int min, int max) { var sb = new StringBuilder(); Random rd = new Random(Environment.TickCount + DateTime.Now.Millisecond); if (isOverlap) { for (int i = 0; i < number; i++) { sb.Append(Convert.ToChar(rd.Next(min, max))); } } else { var Temporary = new List<char>(); for (int i = 0; i < number; i++) { while (true) { var isindex = Convert.ToChar(rd.Next(min, max)); if (Temporary.Contains(isindex)) { continue; } else { Temporary.Add(isindex); sb.Append(isindex); break; } } } } return sb; }
例子 getRandom(不重复true,3个,) max = 123; min = 65;字母 max = 58; min = 48; 数字
来一段儿,这个网上一搜一大把的/// <summary> /// 返回大写字母加数字的随机编码 /// </summary> /// <param name="CodeLength"></param> /// <returns></returns> public static string GetRndCode(int CodeLength) { string Vchar = "0,1,2,3,4,5,6,7,8,9,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"; string[] VcArray = Vchar.Split(','); string RndCode = string.Empty; int temp = -1; //记录上次随机数值 //采用一个简单的算法以保证生成随机数的不同 Random rand = new Random(); for (int i = 0; i < CodeLength; i++) { if (temp != -1) { rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); } int t = rand.Next(35); //如果出现与上次一样的字符,则重新生成一个 if (temp != -1 && temp == t) { return GetRndCode(CodeLength); } temp = t; RndCode += VcArray[t]; } return RndCode; }
Random ra =new Random();
string pt="";
int res=0;
switch (pt)
{
case "3":
res= ra.Next(100, 999);
break;
case "5":
res= ra.Next(10000, 99999);
break;
.....
}
// 生成随机数字
// <param name="length">生成长度</param>
public static string Number(int Length)
{
return Number(Length, false);
} /// 生成随机数字
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
public static string Number(int Length,bool Sleep)
{
if(Sleep)
System.Threading.Thread.Sleep(3);
string result = "";
System.Random random = new Random();
for (int i = 0; i < Length; i++)
{
result += random.Next(10).ToString();
}
return result;
}
/// <summary>
/// 生成随机纯字母随机数
/// </summary>
/// <param name="IntStr">生成长度</param>
/// <returns></returns>
public static string Str_char(int Length)
{
return Str_char(Length, false);
} /// <summary>
/// 生成随机纯字母随机数
/// </summary>
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
/// <returns></returns>
public static string Str_char(int Length, bool Sleep)
{
if (Sleep) System.Threading.Thread.Sleep(3);
char[] Pattern = new char[] { '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' };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
}
/// 字母随机数
/// </summary>
/// <param name="n">生成长度</param>
/// <returns></returns>
public static string RandLetter(int n)
{
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'_',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
};
StringBuilder num = new StringBuilder();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < n; i++)
{
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
}
return num.ToString();
}
/// 字母随机数
/// </summary>
/// <param name="n">生成长度</param>
/// <returns></returns>
public static string RandLetter(int n)
{
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'_',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
};
StringBuilder num = new StringBuilder();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < n; i++)
{
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
}
return num.ToString();
}
string[] s = new string[26]{"a","b","c","d","e","f","g",...}; Random ran = new Random();
int n = 0;
int m=0; string nValue = "";
string mValue = "";
int index = 0;
for(int i = 0;i<n;i++)
{
index = ran.Next(0,25); nValue += s[index];
} for(int i=0;i<m;i++)
{
index = ran.Next(0,9);
mValue += index.ToString();
}
string Table = "abcdefghijklmnopqrstuvwxyz"; int[] p = new int[26];//字符索引
int Length = 3 - 1;//字符串的长度
int i = Length;
p[i] = -1;
int a = 0;
while (a++ <= Length) sb.Append("a");
while (i > -1)
{
if (p[i] == 25)
{
sb.Remove(i, 1);
i--;
continue;
}
else
{
p[i] = p[i] + 1;
sb[i] = Table[p[i]];
if (i < Length)
{
i++;
p[i] = -1;
sb.Append(" ");
continue;
}
}
Response.Write(sb.ToString() + "<br/>");
}
或者把值存入ISET里来查
/// 生成随机数
/// </summary>
/// <param name="pwdLength">参数值</param>
/// <returns></returns>
public static string MakePassword(int PwdLength)
{
string Tmp = "";
string pwd ="abcdefghkmnpqrstuvwxyz23456789ABCDEFGHKMNPQRSTUVWXYZ";
int MakeRandNum;
Random rnd = new Random();
for(int i=0;i<PwdLength;i++)
{
MakeRandNum = rnd.Next(pwd.Length);
Tmp += pwd[MakeRandNum];
}
return Tmp;
}
/// 生成随机字符串
/// </summary>
/// <param name="length">目标字符串的长度</param>
/// <param name="useNum">是否包含数字,1=包含,默认为包含</param>
/// <param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
/// <param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
/// <param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
/// <param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
/// <returns>指定长度的随机字符串</returns>
public string GetRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
{
byte[] b = new byte[4];
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
Random r = new Random(BitConverter.ToInt32(b, 0));
string s = null, str = custom; if (useNum == true) { str += "0123456789"; }
if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; } for (int i = 0; i < length; i++)
{
s += str.Substring(r.Next(0, str.Length - 1), 1);
} return s;
}
<script type="text/javascript" language="javascript">function CreateStr()
{
var j=document.getElementById("input2").value;
if(isNaN(j))
{
alert("请输入整数");
}
var tmpCh = "";
var i=1;
for(i=1;i<=j;i++)
{
tmpCh += String.fromCharCode(Math.floor( Math.random() * 26) + "a".charCodeAt(0));
}
alert(tmpCh);
}
function CreateNo()
{ var j=document.getElementById("input3").value; if(isNaN(j))
{
alert("请输入整数");
}var s = (Math.floor(Math.random()*10000) % j+1).toString();
for(var i=1;i<j; i++)
{
s = s + (Math.floor(Math.random()*10000) % 10).toString();
}
alert(s);
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE></HEAD><BODY>
<input type="input" id="input2" />
<input type="button" onclick="CreateStr()" value="生成随机字母" width="120px" height="28px"/>
<input type="input" id="input3" />
<input type="button" onclick="CreateNo()" value="生成随机数" width="120px" height="28px"/>
</BODY>
</HTML>
private static StringBuilder getRandom( bool isOverlap, int number, int min, int max)
{
var sb = new StringBuilder();
Random rd = new Random(Environment.TickCount + DateTime.Now.Millisecond);
if (isOverlap)
{
for (int i = 0; i < number; i++)
{
sb.Append(Convert.ToChar(rd.Next(min, max)));
}
}
else
{
var Temporary = new List<char>();
for (int i = 0; i < number; i++)
{
while (true)
{
var isindex = Convert.ToChar(rd.Next(min, max));
if (Temporary.Contains(isindex))
{
continue;
}
else
{
Temporary.Add(isindex);
sb.Append(isindex);
break;
}
}
}
}
return sb;
}
例子
getRandom(不重复true,3个,)
max = 123; min = 65;字母
max = 58; min = 48; 数字
/// 返回大写字母加数字的随机编码
/// </summary>
/// <param name="CodeLength"></param>
/// <returns></returns>
public static string GetRndCode(int CodeLength)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9,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"; string[] VcArray = Vchar.Split(',');
string RndCode = string.Empty;
int temp = -1; //记录上次随机数值 //采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
for (int i = 0; i < CodeLength; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
//如果出现与上次一样的字符,则重新生成一个
if (temp != -1 && temp == t)
{
return GetRndCode(CodeLength);
}
temp = t;
RndCode += VcArray[t];
}
return RndCode;
}