fork download
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4.  
  5. /*============================================================
  6. **
  7. **
  8. **
  9. ** Purpose: A random number generator.
  10. **
  11. **
  12. ===========================================================*/
  13. namespace System {
  14.  
  15. using System;
  16. using System.Runtime;
  17. using System.Runtime.CompilerServices;
  18. using System.Globalization;
  19. using System.Diagnostics.Contracts;
  20. [System.Runtime.InteropServices.ComVisible(true)]
  21. [Serializable]
  22. public class Random {
  23. //
  24. // Private Constants
  25. //
  26. private const int MBIG = Int32.MaxValue;
  27. private const int MSEED = 161803398;
  28. private const int MZ = 0;
  29.  
  30.  
  31. //
  32. // Member Variables
  33. //
  34. private int inext;
  35. private int inextp;
  36. private int[] SeedArray = new int[56];
  37.  
  38. //
  39. // Public Constants
  40. //
  41.  
  42. //
  43. // Native Declarations
  44. //
  45.  
  46. //
  47. // Constructors
  48. //
  49.  
  50. public Random()
  51. : this(Environment.TickCount) {
  52. }
  53.  
  54. public Random(int Seed) {
  55. int ii;
  56. int mj, mk;
  57.  
  58. //Initialize our Seed array.
  59. int subtraction = (Seed == Int32.MinValue) ? Int32.MaxValue : Math.Abs(Seed);
  60. mj = MSEED - subtraction;
  61. SeedArray[55]=mj;
  62. mk=1;
  63. for (int i=1; i<55; i++) { //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
  64. ii = (21*i)%55;
  65. SeedArray[ii]=mk;
  66. mk = mj - mk;
  67. if (mk<0) mk+=MBIG;
  68. mj=SeedArray[ii];
  69. }
  70. for (int k=1; k<5; k++) {
  71. for (int i=1; i<56; i++) {
  72. SeedArray[i] -= SeedArray[1+(i+30)%55];
  73. if (SeedArray[i]<0) SeedArray[i]+=MBIG;
  74. }
  75. }
  76. inext=0;
  77. inextp = 21;
  78. Seed = 1;
  79. }
  80.  
  81. //
  82. // Package Private Methods
  83. //
  84.  
  85. /*====================================Sample====================================
  86.   **Action: Return a new random number [0..1) and reSeed the Seed array.
  87.   **Returns: A double [0..1)
  88.   **Arguments: None
  89.   **Exceptions: None
  90.   ==============================================================================*/
  91. protected virtual double Sample() {
  92. //Including this division at the end gives us significantly improved
  93. //random number distribution.
  94. return (InternalSample()*(1.0/MBIG));
  95. }
  96.  
  97. private int InternalSample() {
  98. int retVal;
  99. int locINext = inext;
  100. int locINextp = inextp;
  101.  
  102. if (++locINext >=56) locINext=1;
  103. if (++locINextp>= 56) locINextp = 1;
  104.  
  105. retVal = SeedArray[locINext]-SeedArray[locINextp];
  106.  
  107. if (retVal == MBIG) retVal--;
  108. if (retVal<0) retVal+=MBIG;
  109.  
  110. SeedArray[locINext]=retVal;
  111.  
  112. inext = locINext;
  113. inextp = locINextp;
  114.  
  115. return retVal;
  116. }
  117.  
  118. //
  119. // Public Instance Methods
  120. //
  121.  
  122.  
  123. /*=====================================Next=====================================
  124.   **Returns: An int [0..Int32.MaxValue)
  125.   **Arguments: None
  126.   **Exceptions: None.
  127.   ==============================================================================*/
  128. public virtual int Next() {
  129. return InternalSample();
  130. }
  131.  
  132. private double GetSampleForLargeRange() {
  133. // The distribution of double value returned by Sample
  134. // is not distributed well enough for a large range.
  135. // If we use Sample for a range [Int32.MinValue..Int32.MaxValue)
  136. // We will end up getting even numbers only.
  137.  
  138. int result = InternalSample();
  139. // Note we can't use addition here. The distribution will be bad if we do that.
  140. bool negative = (InternalSample()%2 == 0) ? true : false; // decide the sign based on second sample
  141. if( negative) {
  142. result = -result;
  143. }
  144. double d = result;
  145. d += (Int32.MaxValue - 1); // get a number in range [0 .. 2 * Int32MaxValue - 1)
  146. d /= 2*(uint)Int32.MaxValue - 1 ;
  147. return d;
  148. }
  149.  
  150.  
  151. /*=====================================Next=====================================
  152.   **Returns: An int [minvalue..maxvalue)
  153.   **Arguments: minValue -- the least legal value for the Random number.
  154.   ** maxValue -- One greater than the greatest legal return value.
  155.   **Exceptions: None.
  156.   ==============================================================================*/
  157. public virtual int Next(int minValue, int maxValue) {
  158. if (minValue>maxValue) {
  159. throw new ArgumentOutOfRangeException("minValue",Environment.GetResourceString("Argument_MinMaxValue", "minValue", "maxValue"));
  160. }
  161. Contract.EndContractBlock();
  162.  
  163. long range = (long)maxValue-minValue;
  164. if( range <= (long)Int32.MaxValue) {
  165. return ((int)(Sample() * range) + minValue);
  166. }
  167. else {
  168. return (int)((long)(GetSampleForLargeRange() * range) + minValue);
  169. }
  170. }
  171.  
  172.  
  173. /*=====================================Next=====================================
  174.   **Returns: An int [0..maxValue)
  175.   **Arguments: maxValue -- One more than the greatest legal return value.
  176.   **Exceptions: None.
  177.   ==============================================================================*/
  178. public virtual int Next(int maxValue) {
  179. if (maxValue<0) {
  180. throw new ArgumentOutOfRangeException("maxValue", Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", "maxValue"));
  181. }
  182. Contract.EndContractBlock();
  183. return (int)(Sample()*maxValue);
  184. }
  185.  
  186.  
  187. /*=====================================Next=====================================
  188.   **Returns: A double [0..1)
  189.   **Arguments: None
  190.   **Exceptions: None
  191.   ==============================================================================*/
  192. public virtual double NextDouble() {
  193. return Sample();
  194. }
  195.  
  196.  
  197. /*==================================NextBytes===================================
  198.   **Action: Fills the byte array with random bytes [0..0x7f]. The entire array is filled.
  199.   **Returns:Void
  200.   **Arugments: buffer -- the array to be filled.
  201.   **Exceptions: None
  202.   ==============================================================================*/
  203. public virtual void NextBytes(byte [] buffer){
  204. if (buffer==null) throw new ArgumentNullException("buffer");
  205. Contract.EndContractBlock();
  206. for (int i=0; i<buffer.Length; i++) {
  207. buffer[i]=(byte)(InternalSample()%(Byte.MaxValue+1));
  208. }
  209. }
  210. }
  211.  
  212.  
  213.  
  214. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(159,76): error CS0122: `System.Environment.GetResourceString(string, params object[])' is inaccessible due to its protection level
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
prog.cs(180,77): error CS0122: `System.Environment.GetResourceString(string, params object[])' is inaccessible due to its protection level
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
error CS5001: Program `prog.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 3 error(s), 0 warnings
stdout
Standard output is empty