谢谢

解决方案 »

  1.   

    Max*rnd()+min
    产生Min至Max之间的随机数。
      

  2.   

    Random r = new Random();
                    r.Next();
    具体打开VS按下F1,检索Random,或者信仰万能的百度
      

  3.   

    这里有个例子 生成二十个不同的随机数
            static List<int> GetUniqueRandom()
            {
                List<int> listUniqueRandom = new List<int>();
                Random rand = new Random();
                while(true)
                {
                    int nTemp = rand.Next(1, 51);
                    if (!listUniqueRandom.Contains(nTemp))
                        listUniqueRandom.Add(nTemp);
                    if (listUniqueRandom.Count == 20)
                        break;
                }
                return listUniqueRandom;
            }
      

  4.   

    这个是生成验证码的随机数的你可以参考下
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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;using System.Drawing.Imaging;
    using System.Drawing;
    using System.IO;public partial class UserControls_Gif : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string tmp = RndNum(5).ToUpper();
            this.Session["ImageV"] = tmp;
            HttpCookie a = new HttpCookie("ImageV", tmp);
            Response.Cookies.Add(a);
            this.CreateCheckCodeImage(tmp);     }    private void ValidateCode(string VNum)
        {
            Bitmap Img = null;
            Graphics g = null;
            MemoryStream ms = null;        int gheight = VNum.Length * 15;
            Img = new Bitmap(gheight, 30);
            g = Graphics.FromImage(Img);
            //背景颜色 
            g.Clear(Color.White);
            //文字字体 
            Font f = new Font("Arial Black", 11);
            //文字颜色 
            SolidBrush s = new SolidBrush(Color.Black);
            g.DrawString(VNum, f, s, 3, 3);
            ms = new MemoryStream();
            Img.Save(ms, ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());        g.Dispose();
            Img.Dispose();
            Response.End();
        }    private string RndNum(int VcodeNum)
        {
            string Vchar = "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";
            string[] VcArray = Vchar.Split(new Char[] { ',' });
            string VNum = "";
            int temp = -1;        Random rand = new Random();        for (int i = 1; i < VcodeNum + 1; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }            int t = rand.Next(35);
                if (temp != -1 && temp == t)
                {
                    return RndNum(VcodeNum);
                }
                temp = t;
                VNum += VcArray[t];
            }
            return VNum;
        } 
        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 * 15.5)), 30);
       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", 14, (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();
       }
      }
     }
      

  5.   

    我觉得可以用Random 生成随机数以后再MD5一下然后随机截取你需要的位数的字符串是不是更好呢??