如何生成一串(5个)只包含字母和数字的随即数 ,谢谢

aierduo 2006-12-05 04:42:13
如题
...全文
163 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aierduo 2006-12-05
  • 打赏
  • 举报
回复
谢谢
lovvver 2006-12-05
  • 打赏
  • 举报
回复
System.Guid.NewGuid().ToString().Substring(0,5) ;
aierduo 2006-12-05
  • 打赏
  • 举报
回复
谢谢
lanye_purple 2006-12-05
  • 打赏
  • 举报
回复
也有第三方控件下载.
sskset 2006-12-05
  • 打赏
  • 举报
回复
/// <summary>
/// 获取随机字符串(数字和字母)
/// </summary>
/// <param name="stringLength">字符串长度</param>
/// <returns></returns>
static string GetRandomString(int stringLength)
{
StringBuilder strReturn = new StringBuilder();
Random rd = new Random();
while (strReturn.Length < stringLength)
{
char c = Convert.ToChar(rd.Next((byte)'0', (byte)'z'));

if (char.IsDigit(c) || char.IsLower(c) || char.IsUpper(c))
strReturn.Append(c);
}

return strReturn.ToString();

}
chenz322556 2006-12-05
  • 打赏
  • 举报
回复
private void Page_Load(object sender, System.EventArgs e)
{
string strCode = CreateRandomCode(5);//获取随即数函数
Response.Write(strCode);
}

private string CreateRandomCode(int CodeCount)
{
string allChar = "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[] allCharArray = allChar.Split(Convert.ToChar(","));
string randomCode = "";
int temp = -1;

Random rand = new Random();
for (int i=0;i<CodeCount;i++)
{
if (temp != -1)
{
rand = new Random(temp*i*((int) DateTime.Now.Ticks));
}

int t = rand.Next(35);
while (temp == t)
{
//t = rand.Next(35);
}

temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
king4th0 2006-12-05
  • 打赏
  • 举报
回复
public class MyRandom
{
public static string GetRandom(int strlength)
{
// char[] randomSource = new char[]{'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','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'};
char[] randomSource = new char[]{'0','1', '2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
string num = "";
Random r = new Random();
for(int i = 0; i < strlength; i++)
{
num += randomSource[r.Next(0, randomSource.Length)].ToString();
}
return num;
}
}

把数组变变就行了。

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧