利用Random得到随机数字、字母和字符串

利用Random得到任意数字、字母和字符串最后附上MS的Random的源码。

/// <summary>
/// Get Random number. It is used to get number over int range
/// </summary>
/// <param name="length">
/// the length of number
/// </param>
/// <returns>
/// a string included random numbers
/// </returns>
public static string GetRandomNumber(int length)
{
    Random random = new Random();
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i++)
    {
        sb.Append(random.Next(0, 10));
    }

    return sb.ToString();
}

//**********************************************************

/// <summary>
/// Get random char included number, lowercase, uppercase
/// 48 ~ 57:  0 ~ 9
/// 65 ~ 90:  A ~ Z
/// 97 ~ 122: a ~ z
/// </summary>
/// <returns></returns>
public static char GetRandomChar()
{
    Random random = new Random();
   
    int ret = random.Next(48, 123);
    while ((ret > 57 && ret < 65) || (ret > 90 && ret < 97))
    {
        ret = random.Next(48, 123);
    }
    return (char)ret;
}

//**********************************************************

/// <summary>
/// Get Random String included number, lowercase, uppercase
/// </summary>
/// <param name="length">
/// the length of string
/// </param>
/// <returns></returns>
public static string GetRandomString(int length)
{
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i++)
    {
        sb.Append(getRandomChar());
    }
    return sb.ToString();
}

//**********************************************************

/*============================================================
**
** Class:  Random.cs
**
**                                       
**
** Purpose: A random number generator.
**
** Date:  July 8, 1998
**
===========================================================*/
namespace System
{
              
    using System;
    using System.Runtime.CompilerServices;
    [Serializable()] public class Random
    {
        //
        // Private Constants
        //
        private const int MBIG =  Int32.MaxValue;
        private const int MSEED = 161803398;
        private const int MZ = 0;
                            
                              
        //
        // Member Variables
        //
        private int inext, inextp;
        private int[] SeedArray = new int[56];
                            
        //
        // Public Constants
        //
                            
        //
        // Native Declarations
        //
                            
        //
        // Constructors
        //
           
        public Random()
            : this(Environment.TickCount)
        {
        }
                            
        public Random(int Seed)
        {
            int ii;
            int mj, mk;
                                       
            //Initialize our Seed array.
            //This algorithm comes from Numerical Recipes in C (2nd Ed.)
            mj = MSEED - Math.Abs(Seed);
            SeedArray[55]=mj;
            mk=1;
            for (int i=1; i<55; i++)
            {  //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
                ii = (21*i)%55;
                SeedArray[ii]=mk;
                mk = mj - mk;
                if (mk<0) mk+=MBIG;
                mj=SeedArray[ii];
            }
            for (int k=1; k<5; k++)
            {
                for (int i=1; i<56; i++)
                {
                    SeedArray[i] -= SeedArray[1+(i+30)%55];
                    if (SeedArray[i]<0) SeedArray[i]+=MBIG;
                }
            }
            inext=0;
            inextp = 21;
            Seed = 1;
        }
                            
        //
        // Package Private Methods
        //
                            
        /*====================================Sample====================================
        **Action: Return a new random number [0..1) and reSeed the Seed array.
        **Returns: A double [0..1)
        **Arguments: None
        **Exceptions: None
        ==============================================================================*/
        protected virtual double Sample()
        {
            int retVal;
            int locINext = inext;
            int locINextp = inextp;
                                       
            if (++locINext >=56) locINext=1;
            if (++locINextp>= 56) locINextp = 1;
                                         
            retVal = SeedArray[locINext]-SeedArray[locINextp];
                                         
            if (retVal<0) retVal+=MBIG;
                                         
            SeedArray[locINext]=retVal;
                                       
            inext = locINext;
            inextp = locINextp;
                                                   
            //Including this division at the end gives us significantly improved
            //random number distribution.
            return (retVal*(1.0/MBIG));
        }
                            
        //
        // Public Instance Methods
        //
                            
                            
        /*====================================

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值