(1)
using System;
namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
Random random = new Random();
int a = random.Next(1, 10);
Console.WriteLine(a.ToString());
Console.ReadLine();
}
}
}
(2)
//stringbuilder 是我加的,不要每次都生成一个新对象浪费空间。
public string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
StringBuilder checkcode = new StringBuilder();
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
//如果是奇数 就生成字母,否则生成数字。
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
checkcode.Append(code.ToString());
}
/**////用于客户端校验码比较
Session["CheckCode"] = checkcode;
string check = checkcode.ToString();
code1 = check;
return check;
}
3.
产生62位内任意数字大小写字母的随机数
private static char[] constant=
{
'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'
};
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd= new Random();
for(int i=0;i<Length;i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}
要是只要字母,改为如下:
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);
4.
改造一下,可自由选择生成随机字母类型
private static char[] constant =
{
'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'
};
public enum RandomType
{
All,
Number,
Uppercased,
Lowercased,
NumberAndUppercased,
NumberAndLowercased,
UppercasedAndLowercased,
}
public static string GenerateRandom(int Length,RandomType rt)
{
int initsize=0;
int beginsize = 0;
int endsize=0;
Boolean IsCross=false;
switch (rt)
{
case RandomType.All:
{
initsize = 62;
beginsize = 1;
endsize = 62;
//IsCross = false;
break;
}
case RandomType.Lowercased:
{
initsize = 26;
beginsize = 11;
endsize = 36;
//IsCross = false;
break;
}
case RandomType.Uppercased:
{
initsize = 26;
beginsize = 37;
endsize = 62;
// IsCross = false;
break;
}
case RandomType.Number:
{
initsize = 10;
beginsize = 1;
endsize = 10;
//IsCross = false;
break;
}
case RandomType.UppercasedAndLowercased:
{
initsize = 52;
beginsize = 11;
endsize = 62;
//IsCross = false;
break;
}
case RandomType.NumberAndLowercased:
{
initsize = 36;
beginsize = 1;
endsize = 36;
//IsCross = false;
break;
}
case RandomType.NumberAndUppercased:
{
IsCross = true;
break;
}
}
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(initsize);
Random rd = new Random();
if (!IsCross)
{
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(beginsize, endsize)]);
}
}
else
{
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(1, 10)]);
newRandom.Append(constant[rd.Next(37, 62)]);
}
}
return newRandom.ToString();
}
评论