正文

随机生成数字字母2007-10-10 14:19:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jixian/30009.html

分享到:

(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();
}

阅读(3614) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册