adevs_rand.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * Copyright (c) 2013, James Nutaro
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * The views and conclusions contained in the software and documentation are those
  26. * of the authors and should not be interpreted as representing official policies,
  27. * either expressed or implied, of the FreeBSD Project.
  28. *
  29. * Bugs, comments, and questions can be sent to nutaro@gmail.com
  30. */
  31. #ifndef __adevs_rand_h_
  32. #define __adevs_rand_h_
  33. #include "adevs.h"
  34. #include <cstdlib>
  35. namespace adevs
  36. {
  37. /**
  38. * The random_seq class is an abstract interface to a random sequence
  39. * generator.
  40. */
  41. class random_seq
  42. {
  43. public:
  44. /// Set the seed for the random number generator
  45. virtual void set_seed(unsigned long seed) = 0;
  46. /// Get the next double uniformly distributed in [0, 1]
  47. virtual double next_dbl() = 0;
  48. /// Copy the random number generator
  49. virtual random_seq* copy() const = 0;
  50. /// Get the next unsigned long
  51. virtual unsigned long next_long() = 0;
  52. /// Destructor
  53. virtual ~random_seq(){}
  54. };
  55. /**
  56. * The crand class provides random number sequences using the standard
  57. * C rand_r() function. Each instance of crand generates its own random
  58. * number sequence, and the clone method saves the state of the random
  59. * number generator. This class can be used in parallel simulations.
  60. */
  61. class crand: public random_seq
  62. {
  63. public:
  64. /// Create a generator with the default seed
  65. crand():seedp(0){}
  66. /// Copy constructor
  67. crand(const crand& src):seedp(src.seedp){}
  68. /// Create a generator with the given seed
  69. crand(unsigned long seed):seedp((unsigned int)seed){}
  70. /// Set the seed for the random number generator
  71. void set_seed(unsigned long seed) { seedp = (unsigned int)seed; }
  72. /// Get the next double uniformly distributed in [0, 1]
  73. double next_dbl()
  74. {
  75. #ifdef _WIN32
  76. return ((double)next_long()/(double)UINT_MAX);
  77. #else
  78. return ((double)next_long()/(double)RAND_MAX);
  79. #endif
  80. }
  81. /// Get the next unsigned long
  82. unsigned long next_long();
  83. /// Copy the random number generator
  84. random_seq* copy() const { return new crand(*this); }
  85. /// Destructor
  86. ~crand(){}
  87. private:
  88. unsigned int seedp;
  89. };
  90. /**
  91. * <p>The rv class provides a random variable based on a selectable
  92. * implementation. By default, this implementation is crand.
  93. * I recommend that you find a modern random variate generator,
  94. * such as the ones included in the new C++ standards,
  95. * rather than use the one here.</p>
  96. * <p>The assortment of random variable types was contributed by
  97. * Alex Cave (who, at the time, was with the Intelligent
  98. * Systems Automation Group in the School of Engineering at
  99. * Deakin University).</p>
  100. */
  101. class rv
  102. {
  103. public:
  104. /// Create a random variable with the default implementation.
  105. rv (unsigned long seed = 1);
  106. /**
  107. * Create a random variable with the desired implementation. The
  108. * implementation class is adopted by the rv.
  109. */
  110. rv(random_seq* rand);
  111. /// Copy constructor relies on copy method of underlying stream.
  112. rv(const rv& src);
  113. /// Assignment operator relies on copy method of underlying stream.
  114. const rv& operator=(const rv& src);
  115. /// See the random number generator implementation
  116. void set_seed(unsigned long seed);
  117. /// Get a raw value from the underlying random number generator
  118. unsigned long next_long();
  119. /// Sample a triangular distribution with minimum a, maximum b,
  120. /// and mode c.
  121. double triangular(double a, double b, double c);
  122. /// Sample a uniform distribution in the range [a, b]
  123. double uniform(double a, double b);
  124. /**
  125. * Sample a normally distributed random variable with mean m and
  126. * standard deviation s.
  127. */
  128. double normal(double m, double s);
  129. /**
  130. * return a negative exponentially distributed random number with
  131. * the mean as parameter
  132. */
  133. double exponential(double a);
  134. /**
  135. * return a hyperexponentially distributed random number with
  136. * the means as parameters to two exponentially distributed variates,
  137. * the first with a chance of p of being generated, the second with a
  138. * chance of 1-p.
  139. */
  140. double hyperexponential(double p,double a,double b);
  141. /// Return a laplace number with the given parameter
  142. double laplace(double a);
  143. /// Sample a chisquare random variable with n degrees of freedom
  144. double chisquare(unsigned int n);
  145. /// Sample a student-t random variable
  146. double student(unsigned int n);
  147. /// Sample a lognormal random variable
  148. double lognormal(double a,double b);
  149. /// Sample an erlang distribution
  150. double erlang(unsigned int n,double a);
  151. /// Sample a gamma random variable
  152. double gamma(double a,double b);
  153. /// Sample a beta random variable
  154. double beta(double a,double b);
  155. /// Sample a F-distributed random variable
  156. double fdistribution(unsigned int n,unsigned int m);
  157. /// Sample a Poisson random variable
  158. double poisson(double a);
  159. /// Sample a geometric random variable with event probability p
  160. double geometric(double p);
  161. /**
  162. * return a variate from the hypergeometric distribution with m the
  163. * population, p the chance on success and n the number of items drawn
  164. */
  165. double hypergeometric(unsigned int m,unsigned int n,double p);
  166. /// Sample a weibull random variable
  167. double weibull(double a,double b);
  168. /**
  169. * An event count for a binomial distribution with event
  170. * probability p and n the number of trials
  171. */
  172. double binomial(double p,unsigned int n);
  173. /**
  174. * return a random variable with probabilty of success equal to p
  175. * and n as the number of successes
  176. */
  177. double negativebinomial(double p,unsigned int n);
  178. /// Sample a triangular random variable
  179. double triangular(double a);
  180. /// return TRUE with probability p and FALSE with probability 1-p
  181. bool probability(double p);
  182. /// Sample a lognormal gamma random variable
  183. double lngamma(double xx);
  184. /// Destructor
  185. ~rv();
  186. private:
  187. random_seq* _impl;
  188. typedef enum
  189. {
  190. NDURATION,
  191. NCOUNT,
  192. EMPTY,
  193. NDELAY,
  194. ADDTOQ,
  195. EMPTYQ,
  196. HNCLMS,
  197. HMODE,
  198. PROBVAL,
  199. ERRUNIFORM,
  200. ERRNORMAL,
  201. ERRLOGNORM,
  202. ERRTRIANG,
  203. ERRGAMMA,
  204. ERRBETA,
  205. ERREXPONENT,
  206. ERRERLANG,
  207. ERRHYPGEO,
  208. NULLEV,
  209. NOHISTO,
  210. INITERR,
  211. AMODE,
  212. HFORM,
  213. ERRFILE,
  214. SAMPLE,
  215. FRACTION,
  216. LEVEL,
  217. SCAN,
  218. SUPPRESS,
  219. SEED
  220. } errorType;
  221. void err(errorType n);
  222. };
  223. } // end of namespace
  224. #endif