首页 > 开发 > 综合 > 正文

用C#写的随机数产生类

2024-07-21 02:26:29
字体:
来源:转载
供稿:网友


using system;
using system.collections.generic;
using system.text;
using system.threading;
using system.security.cryptography;

namespace randnumber
...{
     class randnumber
    ...{
        /**//// <summary>
        /// 产生随机数的个数
        /// </summary>
         public  int num;
         /**//// <summary>
         /// 随机数存储的数组
         /// </summary>
         public double[] randnum;

         /**//// <summary>
         /// 构造函数
         /// </summary>
          public  randnumber(int num)
         ...{
             this.num = num;
             randnum = new double[this.num];
         }


         /**//// <summary>
         /// 产生随机数,填充数组
         /// </summary>
         /// <returns>产生0--1之间的随机数,填充数组</returns>
         private     void  rundoublerandoms(random randobj )
        ...{
           
            
             for (int i=0; i < num; i++)
               ...{
                   randnum[i] = randobj.nextdouble();
                
               }
              
                
        }
        /**//// <summary>
        /// 利用系统时间产生随机数
        /// </summary>
        /// <remarks>调用rundoublerandoms方法填充数组</remarks>
        public    void  autoseedrandoms()
         ...{
                        
             random autorand = new random();

             rundoublerandoms(autorand);
         }

         /**//// <summary>
         /// 产生一串随机的密码串
         /// </summary>
         public  string getrandompassword(int length)
         ...{
             byte[] random = new byte[length / 2];
             rngcryptoserviceprovider rng = new rngcryptoserviceprovider();
             rng.getnonzerobytes(random);

             stringbuilder sb = new stringbuilder(length);

             for (int i = 0; i < random.length; i++)
             ...{
                 sb.append(string.format("{0:x2}", random[i]));
             }
             return sb.tostring();
         }

    }
}

 

 

这其中包含一个用。net中的密码生成创建一个随机的密码串

 


上一篇:c#中cookies的存取

下一篇:C#读取excel表格

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表