C#的Random类的源码

None.gif //  ==++==
None.gif
//  
None.gif
//    
None.gif
//     Copyright (c) 2002 Microsoft Corporation.  All rights reserved.
None.gif
//    
None.gif
//     The use and distribution terms for this software are contained in the file
None.gif
//     named license.txt, which can be found in the root of this distribution.
None.gif
//     By using this software in any fashion, you are agreeing to be bound by the
None.gif
//     terms of this license.
None.gif
//    
None.gif
//     You must not remove this notice, or any other, from this software.
None.gif
//    
None.gif
//  
None.gif
//  ==--==
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*============================================================
InBlock.gif**
InBlock.gif** Class:  Random.cs
InBlock.gif**
InBlock.gif**                                        
InBlock.gif**
InBlock.gif** Purpose: A random number generator.
InBlock.gif**
InBlock.gif** Date:  July 8, 1998
InBlock.gif** 
ExpandedBlockEnd.gif===========================================================
*/

ExpandedBlockStart.gifContractedBlock.gif
namespace  System  dot.gif {
InBlock.gif    
InBlock.gif    
using System;
InBlock.gif    
using System.Runtime.CompilerServices;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif    [Serializable()] public class Random dot.gif{
InBlock.gif      
//
InBlock.gif      
// Private Constants 
InBlock.gif      
//
InBlock.gif
      private const int MBIG =  Int32.MaxValue;
InBlock.gif      
private const int MSEED = 161803398;
InBlock.gif      
private const int MZ = 0;
InBlock.gif    
InBlock.gif      
InBlock.gif      
//
InBlock.gif      
// Member Variables
InBlock.gif      
//
InBlock.gif
      private int inext, inextp;
InBlock.gif      
private int[] SeedArray = new int[56];
InBlock.gif    
InBlock.gif      
//
InBlock.gif      
// Public Constants
InBlock.gif      
//
InBlock.gif    
InBlock.gif      
//
InBlock.gif      
// Native Declarations
InBlock.gif      
//
InBlock.gif    
InBlock.gif      
//
InBlock.gif      
// Constructors
InBlock.gif      
//
InBlock.gif
    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Random"]/*' />
InBlock.gif      public Random() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        : 
this(Environment.TickCount) dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Random1"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif      public Random(int Seed) dot.gif{
InBlock.gif        
int ii;
InBlock.gif        
int mj, mk;
InBlock.gif    
InBlock.gif        
//Initialize our Seed array.
InBlock.gif        
//This algorithm comes from Numerical Recipes in C (2nd Ed.)
InBlock.gif
        mj = MSEED - Math.Abs(Seed);
InBlock.gif        SeedArray[
55]=mj;
InBlock.gif        mk
=1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int i=1; i<55; i++dot.gif{  //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
InBlock.gif
          ii = (21*i)%55;
InBlock.gif          SeedArray[ii]
=mk;
InBlock.gif          mk 
= mj - mk;
InBlock.gif          
if (mk<0) mk+=MBIG;
InBlock.gif          mj
=SeedArray[ii];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int k=1; k<5; k++dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif          
for (int i=1; i<56; i++dot.gif{
InBlock.gif        SeedArray[i] 
-= SeedArray[1+(i+30)%55];
InBlock.gif        
if (SeedArray[i]<0) SeedArray[i]+=MBIG;
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

InBlock.gif        inext
=0;
InBlock.gif        inextp 
= 21;
InBlock.gif        Seed 
= 1;
ExpandedSubBlockEnd.gif      }

InBlock.gif    
InBlock.gif      
//
InBlock.gif      
// Package Private Methods
InBlock.gif      
//
InBlock.gif
    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//*====================================Sample====================================
InBlock.gif      **Action: Return a new random number [0..1) and reSeed the Seed array.
InBlock.gif      **Returns: A double [0..1)
InBlock.gif      **Arguments: None
InBlock.gif      **Exceptions: None
ExpandedSubBlockEnd.gif      ==============================================================================
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Sample"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif      protected virtual double Sample() dot.gif{
InBlock.gif          
int retVal;
InBlock.gif          
int locINext = inext;
InBlock.gif          
int locINextp = inextp;
InBlock.gif
InBlock.gif          
if (++locINext >=56) locINext=1;
InBlock.gif          
if (++locINextp>= 56) locINextp = 1;
InBlock.gif          
InBlock.gif          retVal 
= SeedArray[locINext]-SeedArray[locINextp];
InBlock.gif          
InBlock.gif          
if (retVal<0) retVal+=MBIG;
InBlock.gif          
InBlock.gif          SeedArray[locINext]
=retVal;
InBlock.gif
InBlock.gif          inext 
= locINext;
InBlock.gif          inextp 
= locINextp;
InBlock.gif                    
InBlock.gif          
//Including this division at the end gives us significantly improved
InBlock.gif          
//random number distribution.
InBlock.gif
          return (retVal*(1.0/MBIG));
ExpandedSubBlockEnd.gif      }

InBlock.gif    
InBlock.gif      
//
InBlock.gif      
// Public Instance Methods
InBlock.gif      
// 
InBlock.gif
    
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//*=====================================Next=====================================
InBlock.gif      **Returns: An int [0.._int4.MaxValue)
InBlock.gif      **Arguments: None
InBlock.gif      **Exceptions: None.
ExpandedSubBlockEnd.gif      ==============================================================================
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif      public virtual int Next() dot.gif{
InBlock.gif        
return (int)(Sample()*Int32.MaxValue);
ExpandedSubBlockEnd.gif      }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//*=====================================Next=====================================
InBlock.gif      **Returns: An int [minvalue..maxvalue)
InBlock.gif      **Arguments: minValue -- the least legal value for the Random number.
InBlock.gif      **           maxValue -- the greatest legal return value.
InBlock.gif      **Exceptions: None.
ExpandedSubBlockEnd.gif      ==============================================================================
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next1"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif      public virtual int Next(int minValue, int maxValue) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if (minValue>maxValue) dot.gif{
InBlock.gif              
throw new ArgumentOutOfRangeException("minValue",String.Format(Environment.GetResourceString("Argument_MinMaxValue"), "minValue""maxValue"));
ExpandedSubBlockEnd.gif          }

InBlock.gif          
InBlock.gif          
int range = (maxValue-minValue);
InBlock.gif    
InBlock.gif          
//This is the case where we flipped around (e.g. MaxValue-MinValue);
ExpandedSubBlockStart.gifContractedSubBlock.gif
          if (range<0dot.gif{
InBlock.gif              
long longRange = (long)maxValue-(long)minValue;
InBlock.gif              
return (int)(((long)(Sample()*((double)longRange)))+minValue);
ExpandedSubBlockEnd.gif          }

InBlock.gif          
InBlock.gif          
return ((int)(Sample()*(range)))+minValue;
ExpandedSubBlockEnd.gif      }

InBlock.gif    
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//*=====================================Next=====================================
InBlock.gif      **Returns: An int [0..maxValue)
InBlock.gif      **Arguments: maxValue -- the greatest legal return value.
InBlock.gif      **Exceptions: None.
ExpandedSubBlockEnd.gif      ==============================================================================
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next2"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif      public virtual int Next(int maxValue) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if (maxValue<0dot.gif{
InBlock.gif              
throw new ArgumentOutOfRangeException("maxValue", String.Format(Environment.GetResourceString("ArgumentOutOfRange_MustBePositive"), "maxValue"));
ExpandedSubBlockEnd.gif          }

InBlock.gif          
return (int)(Sample()*maxValue);
ExpandedSubBlockEnd.gif      }

InBlock.gif    
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//*=====================================Next=====================================
InBlock.gif      **Returns: A double [0..1)
InBlock.gif      **Arguments: None
InBlock.gif      **Exceptions: None
ExpandedSubBlockEnd.gif      ==============================================================================
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.NextDouble"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif      public virtual double NextDouble() dot.gif{
InBlock.gif        
return Sample();
ExpandedSubBlockEnd.gif      }

InBlock.gif    
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//*==================================NextBytes===================================
InBlock.gif      **Action:  Fills the byte array with random bytes [0..0x7f].  The entire array is filled.
InBlock.gif      **Returns:Void
InBlock.gif      **Arugments:  buffer -- the array to be filled.
InBlock.gif      **Exceptions: None
ExpandedSubBlockEnd.gif      ==============================================================================
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.NextBytes"]/*' />
ExpandedSubBlockStart.gifContractedSubBlock.gif      public virtual void NextBytes(byte [] buffer)dot.gif{
InBlock.gif        
if (buffer==nullthrow new ArgumentNullException("buffer");
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int i=0; i<buffer.Length; i++dot.gif{
InBlock.gif          buffer[i]
=(byte)(Sample()*(Byte.MaxValue+1)); 
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/wannaCNBLOGS/archive/2005/08/04/207086.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值