惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
V
V2EX
博客园 - 司徒正美
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
雷峰网
雷峰网
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
爱范儿
爱范儿
博客园 - 聂微东
量子位
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News

博客园 - wanna

不知是不是年纪大了,常想起博客园,博客堂的旧事.博客堂好像不再存在了,博客园经营的越红火了.还记得这些人,早期的大牛人? 希望新一年会是和平的一年 js类成员笔记 Create your own HttpContext class>> from 123aspx.net mssql keyword,statements Transact-SQL Optimization Tips ..from mssqlcity.com ASP.NET's Data Storage Objects 通过AOP提高代码的封装和可复用性 Tomcat 与 IIS 整合[转] 如果获取一个表列的最大值.. - wanna - 博客园 无法阻止的弹出窗口: - wanna - 博客园 Attribute Regex.cs C#的GUID是怎么样生成的 Ms的DateTime类源码 Ms的Calendar源码 MS的string.cs MS公开的gc.cs源码 MS公开的gchandle.cs源码
C#的Random类的源码
wanna · 2005-08-04 · via 博客园 - wanna

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

namespace System {
    
    
using System;
    
using System.Runtime.CompilerServices;
    
/// <include file='doc\Random.uex' path='docs/doc[@for="Random"]/*' />
    [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
      
//
    
      
/// <include file='doc\Random.uex' path='docs/doc[@for="Random.Random"]/*' />
      public Random() 
        : 
this(Environment.TickCount) {
      }

    
      
/// <include file='doc\Random.uex' path='docs/doc[@for="Random.Random1"]/*' />
      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
      ==============================================================================
*/

      
/// <include file='doc\Random.uex' path='docs/doc[@for="Random.Sample"]/*' />
      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
      
// 
    
    
      
/*=====================================Next=====================================
      **Returns: An int [0.._int4.MaxValue)
      **Arguments: None
      **Exceptions: None.
      ==============================================================================
*/

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

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

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

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

          
          
return ((int)(Sample()*(range)))+minValue;
      }

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

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

          
return (int)(Sample()*maxValue);
      }

    
    
      
/*=====================================Next=====================================
      **Returns: A double [0..1)
      **Arguments: None
      **Exceptions: None
      ==============================================================================
*/

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

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

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

      }

    }




}