生成验证的随机数

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// randomCode 的摘要说明
/// </summary>

public class randomCode
{
    
public randomCode()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }

    
/// <summary>
    
/// 验证码
    
/// </summary>
    
/// <param name="n">验证码的个数</param>
    
/// <returns>返回生成的随机数</returns>

    public string RandomNum(int n) //
    {
        
string strchar = "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[] VcArray = strchar.Split(',');
        
string VNum = "";                    //
        int temp = -1;                       //记录上次随机数值,尽量避免产生几个一样的随机数
                                             
//采用一个简单的算法以保证生成随机数的不同
        Random rand = new Random();
        
for (int i = 1; i < n + 1; i++)
        
{
            
if (temp != -1)
            
{
                rand 
= new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
            }

            
int t = rand.Next(61);
            
if (temp != -1 && temp == t)
            
{
                
return RandomNum(n);
            }

            temp 
= t;
            VNum 
+= VcArray[t];
        }

        
return VNum;//返回生成的随机数
    }

}

posted @ 2007-10-16 17:43  wj-conquer  阅读(203)  评论(0编辑  收藏  举报