随机生成32位数,不重复

来源:互联网 发布:我感受到了网络的魅力 编辑:程序博客网 时间:2024/04/30 15:44

public string pwd()
        {
            string password = "";
            int number;
            char code;
            string checkCode = String.Empty;
            System.Random random = new Random();
            for (int i = 0; i < 18; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('0' + (char)(number % 10));
                checkCode += code.ToString();
            }
            password = checkCode;
            password = password + System.DateTime.Now.ToString("yyyyMMddHHmmss");
            return password;

        }

 

 

生成登录注册时的验证码如下:

参考网址: http://wenku.baidu.com/view/590b36fe04a1b0717fd5dded.html

---------------------.aspx页面代码----------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckCode.aspx.cs" Inherits="TeaTreeWeb.CheckCode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
    <form id="form1" method="post" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

---------------------.cs页面代码------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace TeaTreeWeb
{
    public partial class CheckCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            this.CreateCheckCodeImage(GenerateCheckCode());
        }

        /// <summary>
        /// 生成指定的随机数字和字母
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns></returns>
        public static string Str(int Length, bool Sleep)
        {
            if (Sleep)
                System.Threading.Thread.Sleep(4);
            char[] Pattern = new char[] { '2', '3', '4', '5', '6', '7', '8', '9',
                'a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z',
                'A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'};
            string result = "";
            int n = Pattern.Length;
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result += Pattern[rnd];
            }
            return result;
        }

        private string GenerateCheckCode()
        {
            return Str(4, true);
         /*   int number;
            char code;
            string checkCode = String.Empty;

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

            //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
            Session["CheckCode"] = checkCode;
            return checkCode;*/
        }

        private void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;

            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            Graphics g = Graphics.FromImage(image);

            try
            {
                //生成随机生成器                Random random = new Random();

                //清空图片背景色                g.Clear(Color.White);

                //画图片的背景噪音线                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);

                //画图片的前景噪音点                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                //画图片的边框线                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
}

 

 调用生成验证码函数

<td align="right" bgcolor="#FFFFFF" class="td13"><strong>验证码:</strong></td> <td align="left" bgcolor="#FFFFFF"><asp:TextBox ID = "txtCCode" runat = "server"
                    size="30"
                    style="border: 1px solid #D7D7D7; background-color:#F6F7F9; width: 63px;"/>
              &nbsp;<img id="CodeImg" onclick="this.src='CheckCode.aspx?tmp='+Math.random()" src="CheckCode.aspx"
                                    alt="验证码" />  <input type="button" value="看不清楚,换一张" onclick="changeCC()"/><br /></td></tr>
                    <tr><td align="right" bgcolor="#FFFFFF" class="td13"></td>

 

.cs文件

txtCCode.Attributes.Add("OnFocus", "if(this.value!='') {this.value=''}");

string tbCode = this.txtCCode.Text.ToLower();

原创粉丝点击