c#怎样生成 随机字母 ?
能给我个例子吗?生成随机字母在最后输出label.text能不能输出string类型的?...
能给我个例子吗?
生成 随机字母
在最后输出 label.text 能不能输出 string 类型的? 展开
生成 随机字母
在最后输出 label.text 能不能输出 string 类型的? 展开
5个回答
展开全部
c#随机生成字母,参考代码如下:
public string CheckRandomCode()
{
Random rom = new Random();
char[] allcheckRandom ={'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'};
string Randomcode = "";
for (int i = 0; i < <span style="color: #FF0000;">4</span>;i++)
{
Randomcode +=allcheckRandom[rom.Next(allcheckRandom.Length)];
Session["verty"] = Randomcode;
}
Session["CheckCode"] = Randomcode;
return Randomcode;
}
2013-07-26
展开全部
class Program
{
static void Main(string[] args)
{
//最终字符串
string str = string.Empty;
//获取字符串的长度
int length = 10;
while (length != 0)
{
//累加最终字符串
str = str + Program.GetRandomChar();
length = length - 1;
//进程延迟15毫秒(随机数是靠时间来生成的运行速度太快会获取同一样的字符)
System.Threading.Thread.Sleep(15);
}
Console.WriteLine(str);
Console.ReadLine();
} public static char GetRandomChar()
{
//随机字符
int c = 0;
//生成随机数的类
Random r = new Random();
//先判断获取大写或小写英文字母 如果等于0获取小写英文字母否获取大写英文字母
if (r.Next(2) == 0)
c = r.Next(97, 123);
else
c = r.Next(65, 91);
r = null;
return (char)c;
} }
{
static void Main(string[] args)
{
//最终字符串
string str = string.Empty;
//获取字符串的长度
int length = 10;
while (length != 0)
{
//累加最终字符串
str = str + Program.GetRandomChar();
length = length - 1;
//进程延迟15毫秒(随机数是靠时间来生成的运行速度太快会获取同一样的字符)
System.Threading.Thread.Sleep(15);
}
Console.WriteLine(str);
Console.ReadLine();
} public static char GetRandomChar()
{
//随机字符
int c = 0;
//生成随机数的类
Random r = new Random();
//先判断获取大写或小写英文字母 如果等于0获取小写英文字母否获取大写英文字母
if (r.Next(2) == 0)
c = r.Next(97, 123);
else
c = r.Next(65, 91);
r = null;
return (char)c;
} }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2017-05-28
展开全部
string[] s1 = { "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" };//字符列表
Random rand = new Random();//实例化rand
this.label1.Text = s1[rand.Next(0,s1.Length)];//label.text=数组s1[随机数]
Random rand = new Random();//实例化rand
this.label1.Text = s1[rand.Next(0,s1.Length)];//label.text=数组s1[随机数]
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public string CreateStr(int length) {
string[] array = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string str = string.Empty;
length = 5;
Random rand = new Random();
for (int i = 0; i < length; i++)
{
str += array[rand.Next(0, array.Length)];
}
return str;
}
string[] array = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string str = string.Empty;
length = 5;
Random rand = new Random();
for (int i = 0; i < length; i++)
{
str += array[rand.Next(0, array.Length)];
}
return str;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-26
展开全部
private static char[] constant=
{
'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'
};
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(52);
Random rd= new Random();
for(int i=0;i<Length;i++)
{
newRandom.Append(constant[rd.Next(52)]);
}
return newRandom.ToString();
}
//调用---------------
sting str=GenerateRandom(6);
{
'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'
};
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(52);
Random rd= new Random();
for(int i=0;i<Length;i++)
{
newRandom.Append(constant[rd.Next(52)]);
}
return newRandom.ToString();
}
//调用---------------
sting str=GenerateRandom(6);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |