PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/verve/base/Applications/Benchmarks/SpecWeb99/webfiles/random.cs

https://bitbucket.org/jdm7dvmoore/msft-research-verve
C# | 356 lines | 212 code | 47 blank | 97 comment | 21 complexity | 61e3f1c8f29eb7e15db607b0f5157df3 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. #define NO_ZIPF_SPIKE
  2. //
  3. // (C)1997 Standard Performance Evaluation Corporation (SPEC)
  4. //
  5. // This suite contains code acquired from several sources who
  6. // understand and agree with SPEC's goal of creating fair and
  7. // objective benchmarks to measure computer performance.
  8. //
  9. // This copyright notice is placed here only to protect SPEC in the
  10. // event the source is misused in any manner that is contrary to
  11. // the spirit, the goals and the intent of SPEC.
  12. //
  13. // The source code is provided to the user or company under the
  14. // license agreement for the SPEC Benchmark Suite for this suite.
  15. //
  16. //////////////////////////////////////////////////////////////////
  17. // *
  18. // Copyright 1991,1992 Legato Systems, Inc. *
  19. // Copyright 1991,1992 Auspex Systems, Inc. *
  20. // Copyright 1991,1992 Data General Corporation *
  21. // Copyright 1991,1992 Digital Equipment Corporation *
  22. // Copyright 1991,1992 Interphase Corporation *
  23. // Copyright 1991,1992 Sun Microsystems, Inc. *
  24. // *
  25. /////////////////////////////////////////////////////////////////
  26. //
  27. // ---------------------- laddis_c_rnd.c ---------------------
  28. //
  29. // Random number generator.
  30. //
  31. //.Exported_routines
  32. // double Spec_random (RandomState *theState);
  33. // long Spec_nrandom (RandomState *theState);
  34. // void Spec_srandom (RandomState *theState, int seed);
  35. //
  36. //.Local_routines
  37. // None
  38. //
  39. //.Revision_History
  40. // 06-Nov-05 Convert to Sing#
  41. // 24-May-97 Re-write to make thread-safe
  42. // 28-Nov-91 ANSI C
  43. // 01-Aug-91 laddis_srandom() and laddis_random()
  44. // now use spec_srand() and spec_rand()
  45. // instead of srandom() and random().
  46. // 17-Apr-91 Created.
  47. //
  48. //
  49. // Here's the source for the random number generator that SPEC uses.
  50. // The function to be called is "spec_rand" which returns an integer
  51. // between 1 and MAX_INT-1.
  52. //
  53. //
  54. //////////////////////////////////////////////////////////////////////////////
  55. // UNIFORM Distribution *
  56. /////////////////////////////////////////////////////////////////////////////
  57. using System;
  58. using Singularity.Application.SPECWeb99;
  59. //using Microsoft.Singularity.Math;
  60. //using Microsoft.Singularity;
  61. //using Microsoft.Singularity.Directory;
  62. //using Microsoft.Singularity.V1.Services;
  63. //using Microsoft.Singularity.Io;
  64. #if SINGULARITY
  65. using Microsoft.Contracts;
  66. #endif
  67. namespace Singularity.Application.SPECWeb99.WebFiles
  68. {
  69. public class Random
  70. {
  71. public const int A_MULTIPLIER = 16807;
  72. public const int M_MODULUS = 2147483647; // (2**31)-1
  73. public const int Q_QUOTIENT = 127773; // 2147483647 / 16807
  74. public const int R_REMAINDER = 2836; // 2147483647 % 16807
  75. //
  76. //Compute the next random number.
  77. //
  78. public class RandomState
  79. {
  80. public int val;
  81. public RandomState(int val)
  82. {
  83. this.val = val;
  84. }
  85. }
  86. public class ZipfState
  87. {
  88. public RandomState rstate;
  89. public int size;
  90. public double []table;
  91. #if SINGULARITY
  92. [NotDelayed]
  93. #endif
  94. public ZipfState (RandomState rstate, int size)
  95. {
  96. this.size = size;
  97. this.rstate = rstate;
  98. table= new double[size+1];
  99. if (table == null) {
  100. Console.WriteLine("ZipfState constructor: can't create {0} entries for table",
  101. size+1);
  102. }
  103. }
  104. } // ZipfState
  105. #if STATIC_NORMAL_DIST
  106. public class NormalState {
  107. public RandomState rstate;
  108. public int size;
  109. public int []table;
  110. };
  111. #else
  112. public class NormalState {
  113. public RandomState rstate;
  114. public double mean, stddev, y2;
  115. public bool use_last;
  116. };
  117. #endif
  118. public double Spec_random(RandomState /*!*/ theState)
  119. // See "Random Number Generators: Good Ones Are Hard To Find",
  120. // Park & Miller, CACM 31#10 October 1988 pages 1192-1201.
  121. ///////////////////////////////////////////////////////////
  122. // THIS IMPLEMENTATION REQUIRES AT LEAST 32 BIT INTEGERS !
  123. ///////////////////////////////////////////////////////////
  124. {
  125. int lo;
  126. int hi;
  127. int test;
  128. hi = theState.val / Q_QUOTIENT;
  129. lo = theState.val % Q_QUOTIENT;
  130. test = A_MULTIPLIER * lo - R_REMAINDER * hi;
  131. if (test > 0) {
  132. theState.val = test;
  133. }
  134. else {
  135. theState.val = test + M_MODULUS;
  136. }
  137. return((float) theState.val / M_MODULUS);
  138. }
  139. //
  140. //Seed the random number generator.
  141. //
  142. public void Spec_srandom( RandomState /*!*/ theState, int seed ) {
  143. theState.val = seed;
  144. }
  145. //
  146. //Returns a random number.
  147. //
  148. public int Spec_nrandom( RandomState/*!*/ theState ) {
  149. Spec_random(theState);
  150. return(theState.val);
  151. }
  152. //////////////////////////////////////////////////////////////////////////////
  153. //ZIPF Distribution *
  154. ////////////////////////////////////////////////////////////////////////////
  155. public ZipfState spec_zipf_setup(RandomState rstate, int size, double Z)
  156. {
  157. int i;
  158. double zipf_sum;
  159. ZipfState theState = new ZipfState(rstate, size);
  160. if (theState == null) return null;
  161. // compute zipf values for samples 1-n
  162. for (i = 1; i <= size; i++) {
  163. theState.table[i] = Math.Pow(((double)1.0/((double)i)), Z);
  164. //theState.table[i] = 1; // SPL TOTAL HACK until POW works!!
  165. }
  166. // sum the values so we can compute probabilities.
  167. // at the same time, make the values cumulative
  168. zipf_sum = 0.0;
  169. for (i = 1; i <= size; i++) {
  170. zipf_sum += theState.table[i] ;
  171. theState.table[i] = zipf_sum;
  172. }
  173. theState.table[size] = 0.0;
  174. theState.table[0] = 0.0;
  175. // compute probability values by dividing by the sum.
  176. // also reverse the table so we have values starting at 1.0
  177. // and descending to 0.0 (this is what spec_zipf needs)
  178. for (i = 0; i < size; i++) {
  179. theState.table[i] = 1.0 - (theState.table[i]/zipf_sum);
  180. }
  181. return theState;
  182. }
  183. private void spec_zipf_free( ZipfState/*!*/ theState) {
  184. if (theState.table != null) {
  185. theState.table = null;
  186. }
  187. }
  188. public int spec_zipf(ZipfState/*!*/ theState) {
  189. double r;
  190. int i;
  191. #if NO_ZIPF_SPIKE
  192. do{
  193. #endif
  194. r = Spec_random(theState.rstate);
  195. i = 0;
  196. while (r < theState.table[i]) {
  197. i++;
  198. }
  199. #if NO_ZIPF_SPIKE
  200. } while (i > theState.size);
  201. #endif
  202. return i-1;
  203. }
  204. #if STATIC_NORMAL_DIST
  205. // Right now, mean and stddev are ignored. If someone has a good function to
  206. // generate the cdf for normal distributions, let me know...
  207. // size=20, mean = 10, stddev = 3.5, will generate numbers from 0 to 20
  208. NormalState spec_normal_setup(, RandomState rstate, double mean, double stddev) {
  209. double normal_dist[] = {
  210. 0.002137432,
  211. 0.005064024,
  212. 0.011135458,
  213. 0.022750062,
  214. 0.043238098,
  215. 0.076563771,
  216. 0.126549006,
  217. 0.19568292,
  218. 0.283854542,
  219. 0.387548544,
  220. 0.5,
  221. 0.612451456,
  222. 0.716145458,
  223. 0.80431708,
  224. 0.873450994,
  225. 0.923436229,
  226. 0.956761902,
  227. 0.977249938,
  228. 0.988864542,
  229. 0.994935976,
  230. 1
  231. };
  232. int i, index = 0;
  233. theState.size = 1000;
  234. theState.rstate = rstate;
  235. theState.table=(int *)malloc(sizeof(int)*(theState.size+1));
  236. if (theState.table == NULL) {
  237. fprintf(stderr, "spec_normal_setup: can't malloc %d bytes for table\n",
  238. sizeof(double)*theState.size);
  239. exit (1);
  240. }
  241. for (i = 0; i < theState.size; i++) {
  242. if ((double)i / (double)theState.size > normal_dist[index]) {
  243. index++;
  244. }
  245. theState.table[i] = index;
  246. }
  247. return theState;
  248. }
  249. void spec_normal_free(NormalState theState) {
  250. if (theState.table) {
  251. free(theState.table);
  252. }
  253. }
  254. int spec_nnormal(NormalState theState) {
  255. int rval = spec_nrandom(theState.rstate.val);
  256. rval = rval % theState.size;
  257. return theState.table[rval];
  258. }
  259. #else
  260. // Guts of this routine are based on:
  261. // boxmuller.c Implements the Polar form of the Box-Muller
  262. // Transformation
  263. //
  264. // (c) Copyright 1994, Everett F. Carter Jr.
  265. // Permission is granted by the author to use
  266. // this software for any application provided this
  267. // copyright notice is preserved.
  268. //
  269. //
  270. private NormalState spec_normal_setup(NormalState/*!*/ theState,
  271. RandomState/*!*/ rstate,
  272. double mean,
  273. double stddev)
  274. {
  275. theState.mean = mean;
  276. theState.stddev = stddev;
  277. theState.rstate = rstate;
  278. theState.use_last = false;
  279. return null;
  280. }
  281. void spec_normal_free(NormalState theState)
  282. {
  283. }
  284. private double Spec_normal(NormalState/*!*/ theState)
  285. {
  286. double x1, x2, w, y1;
  287. if (theState.use_last) { // use value from previous call
  288. y1 = theState.y2;
  289. }
  290. else {
  291. do {
  292. x1 = 2.0 * Spec_random(theState.rstate) - 1.0;
  293. x2 = 2.0 * Spec_random(theState.rstate) - 1.0;
  294. w = x1 * x1 + x2 * x2;
  295. } while (w >= 1.0);
  296. w = Math.Sqrt( (-2.0 * Math.Log( w ) ) / w );
  297. y1 = x1 * w;
  298. theState.y2 = x2 * w;
  299. }
  300. theState.use_last = !(theState.use_last);
  301. return( theState.mean + y1 * theState.stddev );
  302. }
  303. private int spec_nnormal(NormalState/*!*/ theState)
  304. {
  305. return (int)(Spec_normal(theState));
  306. }
  307. #endif
  308. } //random
  309. } //namespace