andrew4582

System.Random

Sep 4th, 2010
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. namespace System
  2. {
  3.     using System.Runtime;
  4.     using System.Runtime.InteropServices;
  5.  
  6.     [Serializable, ComVisible(true)]
  7.     public class Random
  8.     {
  9.         private int inext;
  10.         private int inextp;
  11.         private const int MBIG = 0x7fffffff;
  12.         private const int MSEED = 0x9a4ec86;
  13.         private const int MZ = 0;
  14.         private int[] SeedArray;
  15.  
  16.         public Random() : this(Environment.TickCount)
  17.         {
  18.         }
  19.  
  20.         public Random(int Seed)
  21.         {
  22.             this.SeedArray = new int[0x38];
  23.             int num4 = (Seed == -2147483648) ? 0x7fffffff : Math.Abs(Seed);
  24.             int num2 = 0x9a4ec86 - num4;
  25.             this.SeedArray[0x37] = num2;
  26.             int num3 = 1;
  27.             for (int i = 1; i < 0x37; i++)
  28.             {
  29.                 int index = (0x15 * i) % 0x37;
  30.                 this.SeedArray[index] = num3;
  31.                 num3 = num2 - num3;
  32.                 if (num3 < 0)
  33.                 {
  34.                     num3 += 0x7fffffff;
  35.                 }
  36.                 num2 = this.SeedArray[index];
  37.             }
  38.             for (int j = 1; j < 5; j++)
  39.             {
  40.                 for (int k = 1; k < 0x38; k++)
  41.                 {
  42.                     this.SeedArray[k] -= this.SeedArray[1 + ((k + 30) % 0x37)];
  43.                     if (this.SeedArray[k] < 0)
  44.                     {
  45.                         this.SeedArray[k] += 0x7fffffff;
  46.                     }
  47.                 }
  48.             }
  49.             this.inext = 0;
  50.             this.inextp = 0x15;
  51.             Seed = 1;
  52.         }
  53.  
  54.         private double GetSampleForLargeRange()
  55.         {
  56.             int num = this.InternalSample();
  57.             if ((((this.InternalSample() % 2) == 0) ? 1 : 0) != 0)
  58.             {
  59.                 num = -num;
  60.             }
  61.             double num2 = num;
  62.             num2 += 2147483646.0;
  63.             return (num2 / 4294967293);
  64.         }
  65.  
  66.         private int InternalSample()
  67.         {
  68.             int inext = this.inext;
  69.             int inextp = this.inextp;
  70.             if (++inext >= 0x38)
  71.             {
  72.                 inext = 1;
  73.             }
  74.             if (++inextp >= 0x38)
  75.             {
  76.                 inextp = 1;
  77.             }
  78.             int num = this.SeedArray[inext] - this.SeedArray[inextp];
  79.             if (num == 0x7fffffff)
  80.             {
  81.                 num--;
  82.             }
  83.             if (num < 0)
  84.             {
  85.                 num += 0x7fffffff;
  86.             }
  87.             this.SeedArray[inext] = num;
  88.             this.inext = inext;
  89.             this.inextp = inextp;
  90.             return num;
  91.         }
  92.  
  93.         [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
  94.         public virtual int Next()
  95.         {
  96.             return this.InternalSample();
  97.         }
  98.  
  99.         public virtual int Next(int maxValue)
  100.         {
  101.             if (maxValue < 0)
  102.             {
  103.                 throw new ArgumentOutOfRangeException("maxValue", Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", new object[] { "maxValue" }));
  104.             }
  105.             return (int) (this.Sample() * maxValue);
  106.         }
  107.  
  108.         public virtual int Next(int minValue, int maxValue)
  109.         {
  110.             if (minValue > maxValue)
  111.             {
  112.                 throw new ArgumentOutOfRangeException("minValue", Environment.GetResourceString("Argument_MinMaxValue", new object[] { "minValue", "maxValue" }));
  113.             }
  114.             long num = maxValue - minValue;
  115.             if (num <= 0x7fffffffL)
  116.             {
  117.                 return (((int) (this.Sample() * num)) + minValue);
  118.             }
  119.             return (((int) ((long) (this.GetSampleForLargeRange() * num))) + minValue);
  120.         }
  121.  
  122.         public virtual void NextBytes(byte[] buffer)
  123.         {
  124.             if (buffer == null)
  125.             {
  126.                 throw new ArgumentNullException("buffer");
  127.             }
  128.             for (int i = 0; i < buffer.Length; i++)
  129.             {
  130.                 buffer[i] = (byte) (this.InternalSample() % 0x100);
  131.             }
  132.         }
  133.  
  134.         [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
  135.         public virtual double NextDouble()
  136.         {
  137.             return this.Sample();
  138.         }
  139.  
  140.         [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
  141.         protected virtual double Sample()
  142.         {
  143.             return (this.InternalSample() * 4.6566128752457969E-10);
  144.         }
  145.     }
  146. }
Add Comment
Please, Sign In to add comment