比如WINFORM里设置一个按钮,按一下 lable的值就随机产生15位随机数。怎样做到?
求高手赐教!万分感激!!!!!!

解决方案 »

  1.   

    Random里有一个Next()函数
    Random.Next (Int32, Int32)  Returns a random number within a specified range.  
    可以设置
    UInt32=Random.Next(0x7000,0x7FFF)
    就是15位了,生成后去掉最高位(最高位是0)
      

  2.   

    public static string RandCode(int n) 
    { char[] arrChar = new char[]{ '0','1','2','3','4','5','6','7','8','9' }; StringBuilder num = new StringBuilder(); Random rnd = new Random(DateTime.Now.Millisecond);
     
    for (int i = 0; i < n; i++) { num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); 

    return num.ToString(); 
    }
      

  3.   

    来个凶悍的
    摘至:http://www.cnblogs.com/nclly/archive/2008/12/19/1357961.html///<summary>
    ///得到随机字符.
    ///</summary>
    ///<param name="intLength">Length of the int.</param>
    ///<param name="booNumber">if set to <c>true</c> [boo number].</param>
    ///<param name="booSign">if set to <c>true</c> [boo sign].</param>
    ///<param name="booSmallword">if set to <c>true</c> [boo smallword].</param>
    ///<param name="booBigword">if set to <c>true</c> [boo bigword].</param>
    ///<returns></returns>
    public string getRandomizer(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword)
    {
     //定义
     Random ranA = new Random();
     int intResultRound = 0;
     int intA = 0;
     string strB = "";
     while (intResultRound < intLength)
     {
      //生成随机数A,表示生成类型
      //1=数字,2=符号,3=小写字母,4=大写字母
      intA = ranA.Next(1, 5);
      //如果随机数A=1,则运行生成数字
      //生成随机数A,范围在0-10
      //把随机数A,转成字符
      //生成完,位数+1,字符串累加,结束本次循环
      if (intA == 1 && booNumber)
      {
       intA = ranA.Next(0, 10);
       strB = intA.ToString() + strB;
       intResultRound = intResultRound + 1;
       continue;
      }
      //如果随机数A=2,则运行生成符号
      //生成随机数A,表示生成值域
      //1:33-47值域,2:58-64值域,3:91-96值域,4:123-126值域
      if (intA == 2 && booSign == true)
      {
       intA = ranA.Next(1, 5);
       //如果A=1
       //生成随机数A,33-47的Ascii码
       //把随机数A,转成字符
       //生成完,位数+1,字符串累加,结束本次循环
       if (intA == 1)
       {
        intA = ranA.Next(33, 48);
        strB = ((char)intA).ToString() + strB;
        intResultRound = intResultRound + 1;
        continue;
       }   //如果A=2
       //生成随机数A,58-64的Ascii码
       //把随机数A,转成字符
       //生成完,位数+1,字符串累加,结束本次循环
       if (intA == 2)
       {
        intA = ranA.Next(58, 65);
        strB = ((char)intA).ToString() + strB;
        intResultRound = intResultRound + 1;
        continue;
       }   //如果A=3
       //生成随机数A,91-96的Ascii码
       //把随机数A,转成字符
       //生成完,位数+1,字符串累加,结束本次循环
       if (intA == 3)
       {
        intA = ranA.Next(91, 97);
        strB = ((char)intA).ToString() + strB;
        intResultRound = intResultRound + 1;
        continue;
       }   //如果A=4
       //生成随机数A,123-126的Ascii码
       //把随机数A,转成字符
       //生成完,位数+1,字符串累加,结束本次循环
       if (intA == 4)
       {
        intA = ranA.Next(123, 127);
        strB = ((char)intA).ToString() + strB;
        intResultRound = intResultRound + 1;
        continue;
       }
      }  //如果随机数A=3,则运行生成小写字母
      //生成随机数A,范围在97-122
      //把随机数A,转成字符
      //生成完,位数+1,字符串累加,结束本次循环
      if (intA == 3 && booSmallword == true)
      {
       intA = ranA.Next(97, 123);
       strB = ((char)intA).ToString() + strB;
       intResultRound = intResultRound + 1;
       continue;
      }  //如果随机数A=4,则运行生成大写字母
      //生成随机数A,范围在65-90
      //把随机数A,转成字符
      //生成完,位数+1,字符串累加,结束本次循环
      if (intA == 4 && booBigword == true)
      {
       intA = ranA.Next(65, 89);
       strB = ((char)intA).ToString() + strB;
       intResultRound = intResultRound + 1;
       continue;
      }
     }
     return strB;
    }//随机字符串生成器的主要功能如下: 
    //1、支持自定义字符串长度
    //2、支持自定义是否包含数字
    //3、支持自定义是否包含小写字母
    //4、支持自定义是否包含大写字母
    //5、支持自定义是否包含特殊符号
    //6、支持自定义字符集///<summary>
    ///生成随机字符串
    ///</summary>
    ///<param name="length">目标字符串的长度</param>
    ///<param name="useNum">是否包含数字,1=包含,默认为包含</param>
    ///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
    ///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
    ///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
    ///<param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
    ///<returns>指定长度的随机字符串</returns>
    public string GetRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
    {
     byte[] b = new byte[4];
     new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
     Random r = new Random(BitConverter.ToInt32(b, 0));
     string s = null, str = custom;
     if (useNum == true) { str += "0123456789"; }
     if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
     if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
     if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
     for (int i = 0; i < length; i++)
     {
      s += str.Substring(r.Next(0, str.Length - 1), 1);
     }
     return s;
    }
      

  4.   

    大锅,请问下你(DateTime.Now.Millisecon¡­
    是什么意思
      

  5.   


    const string NUMS = "0123456789";
    static string GernerateNumber(int length)
    {
    Random rnd = new Random(Environment.TickCount);
    char[] result = new char[length];
    for (int i = 0; i < length; i++) result[i] = NUMS[rnd.Next(NUMS.Length)];
    return new string(result);
    }
      

  6.   


    private string GenerateCheckCode()
            {  //产生15位的随机字符串
                int number;
                char code;
                string checkCode = String.Empty;            System.Random random = new Random();            for (int i = 0; i < 15; i++)
                {
                    number = random.Next();                
                    code = (char)('0' + (char)(number % 10));
                    checkCode += code.ToString();
                }            return checkCode;
            }
      

  7.   

    private static int getNewSeed()
        {
            byte[] rndBytes = new byte[4];
             System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(rndBytes);
            return BitConverter.ToInt32(rndBytes, 0);
        }     
    static public string GetRandomString(int len)
        {
           string s = "123456789";
           string Value = string.Empty;
            Random rnd = new Random(getNewSeed());
            while (Value.Length < len)
            {
                string s1 = s[rnd.Next(0, s.Length)].ToString();
                if (Value.IndexOf(s1) == -1) Value += s1;
            }
           return Value;
        }
    Hashtable hashtable = new Hashtable(); 
    Random rm = new Random(); 
    int RmNum = 15; 
    for (int i = 0; hashtable.Count < RmNum; i++) 

        int nValue = rm.Next(100); 
        if (!hashtable.ContainsValue(nValue) && nValue != 0) 
        { 
         hashtable.Add(nValue, nValue); 
         Console.WriteLine(nValue.ToString()); 
        } 
    }
      

  8.   

    random.next(0,10) 循环15次 放入一个字符串中  就可以了 
      

  9.   

            private static void GenerateCheckCode()
            {  //产生15位的随机字符串
                int number;
                string checkCode = String.Empty;            System.Random random = new Random();            for (int i = 0; i < 15; i++)
                {
                    number = random.Next(0, 10);                checkCode += number.ToString();
                }            Console.WriteLine(checkCode);
                        }
      

  10.   

      支持。顶到底
     We shoule do so!!!
    就该这样做啦楼上的!!
      

  11.   

    LS的方法很多,在load事件label.Test=定义的生成谁机数的函数
      

  12.   

    int[] arr = new int[15];
            Random ram = new Random();
            ArrayList list = new ArrayList();
            while (list.Count < 15)
            {
                int num = ram.Next(1, 16);
                if (!list.Contains(num))
                {
                    list.Add(num);
                }
            }
            for (int i = 0; i < 15; i++)
            {
                arr[i] = (int)list[i];
               Label1.Text += arr[i].ToString();
            }
    不知道行不行