@@ -11,7 +11,7 @@ use rand_distr::{Beta, Exp, Gamma, LogNormal, Normal, Triangular, Uniform, Weibu
1111// Discrete distributions
1212use rand_distr:: { Bernoulli , Geometric , Poisson , WeightedIndex } ;
1313
14- use super :: UniformRNG ;
14+ use super :: dynamic_rng :: DynRng ;
1515use crate :: utils:: errors:: SimulationError ;
1616
1717#[ derive( Debug , Clone , Serialize , Deserialize ) ]
@@ -66,29 +66,24 @@ impl Continuous {
6666 /// The generation of random variates drives stochastic behaviors during
6767 /// simulation execution. This function requires the random number
6868 /// generator of the simulation, and produces a f64 random variate.
69- pub fn random_variate ( & mut self , uniform_rng : & mut UniformRNG ) -> Result < f64 , SimulationError > {
69+ pub fn random_variate ( & mut self , uniform_rng : DynRng ) -> Result < f64 , SimulationError > {
70+ let mut rng = ( * uniform_rng) . borrow_mut ( ) ;
7071 match self {
71- Continuous :: Beta { alpha, beta } => {
72- Ok ( Beta :: new ( * alpha, * beta) ?. sample ( uniform_rng. rng ( ) ) )
73- }
74- Continuous :: Exp { lambda } => Ok ( Exp :: new ( * lambda) ?. sample ( uniform_rng. rng ( ) ) ) ,
75- Continuous :: Gamma { shape, scale } => {
76- Ok ( Gamma :: new ( * shape, * scale) ?. sample ( uniform_rng. rng ( ) ) )
77- }
72+ Continuous :: Beta { alpha, beta } => Ok ( Beta :: new ( * alpha, * beta) ?. sample ( & mut * rng) ) ,
73+ Continuous :: Exp { lambda } => Ok ( Exp :: new ( * lambda) ?. sample ( & mut * rng) ) ,
74+ Continuous :: Gamma { shape, scale } => Ok ( Gamma :: new ( * shape, * scale) ?. sample ( & mut * rng) ) ,
7875 Continuous :: LogNormal { mu, sigma } => {
79- Ok ( LogNormal :: new ( * mu, * sigma) ?. sample ( uniform_rng . rng ( ) ) )
76+ Ok ( LogNormal :: new ( * mu, * sigma) ?. sample ( & mut * rng) )
8077 }
8178 Continuous :: Normal { mean, std_dev } => {
82- Ok ( Normal :: new ( * mean, * std_dev) ?. sample ( uniform_rng . rng ( ) ) )
79+ Ok ( Normal :: new ( * mean, * std_dev) ?. sample ( & mut * rng) )
8380 }
8481 Continuous :: Triangular { min, max, mode } => {
85- Ok ( Triangular :: new ( * min, * max, * mode) ?. sample ( uniform_rng. rng ( ) ) )
86- }
87- Continuous :: Uniform { min, max } => {
88- Ok ( Uniform :: new ( * min, * max) . sample ( uniform_rng. rng ( ) ) )
82+ Ok ( Triangular :: new ( * min, * max, * mode) ?. sample ( & mut * rng) )
8983 }
84+ Continuous :: Uniform { min, max } => Ok ( Uniform :: new ( * min, * max) . sample ( & mut * rng) ) ,
9085 Continuous :: Weibull { shape, scale } => {
91- Ok ( Weibull :: new ( * shape, * scale) ?. sample ( uniform_rng . rng ( ) ) )
86+ Ok ( Weibull :: new ( * shape, * scale) ?. sample ( & mut * rng) )
9287 }
9388 }
9489 }
@@ -98,12 +93,10 @@ impl Boolean {
9893 /// The generation of random variates drives stochastic behaviors during
9994 /// simulation execution. This function requires the random number
10095 /// generator of the simulation, and produces a boolean random variate.
101- pub fn random_variate (
102- & mut self ,
103- uniform_rng : & mut UniformRNG ,
104- ) -> Result < bool , SimulationError > {
96+ pub fn random_variate ( & mut self , uniform_rng : DynRng ) -> Result < bool , SimulationError > {
97+ let mut rng = ( * uniform_rng) . borrow_mut ( ) ;
10598 match self {
106- Boolean :: Bernoulli { p } => Ok ( Bernoulli :: new ( * p) ?. sample ( uniform_rng . rng ( ) ) ) ,
99+ Boolean :: Bernoulli { p } => Ok ( Bernoulli :: new ( * p) ?. sample ( & mut * rng) ) ,
107100 }
108101 }
109102}
@@ -112,15 +105,12 @@ impl Discrete {
112105 /// The generation of random variates drives stochastic behaviors during
113106 /// simulation execution. This function requires the random number
114107 /// generator of the simulation, and produces a u64 random variate.
115- pub fn random_variate ( & mut self , uniform_rng : & mut UniformRNG ) -> Result < u64 , SimulationError > {
108+ pub fn random_variate ( & mut self , uniform_rng : DynRng ) -> Result < u64 , SimulationError > {
109+ let mut rng = ( * uniform_rng) . borrow_mut ( ) ;
116110 match self {
117- Discrete :: Geometric { p } => Ok ( Geometric :: new ( * p) ?. sample ( uniform_rng. rng ( ) ) ) ,
118- Discrete :: Poisson { lambda } => {
119- Ok ( Poisson :: new ( * lambda) ?. sample ( uniform_rng. rng ( ) ) as u64 )
120- }
121- Discrete :: Uniform { min, max } => {
122- Ok ( Uniform :: new ( * min, * max) . sample ( uniform_rng. rng ( ) ) )
123- }
111+ Discrete :: Geometric { p } => Ok ( Geometric :: new ( * p) ?. sample ( & mut * rng) ) ,
112+ Discrete :: Poisson { lambda } => Ok ( Poisson :: new ( * lambda) ?. sample ( & mut * rng) as u64 ) ,
113+ Discrete :: Uniform { min, max } => Ok ( Uniform :: new ( * min, * max) . sample ( & mut * rng) ) ,
124114 }
125115 }
126116}
@@ -129,21 +119,21 @@ impl Index {
129119 /// The generation of random variates drives stochastic behaviors during
130120 /// simulation execution. This function requires the random number
131121 /// generator of the simulation, and produces a usize random variate.
132- pub fn random_variate (
133- & mut self ,
134- uniform_rng : & mut UniformRNG ,
135- ) -> Result < usize , SimulationError > {
122+ pub fn random_variate ( & mut self , uniform_rng : DynRng ) -> Result < usize , SimulationError > {
123+ let mut rng = ( * uniform_rng) . borrow_mut ( ) ;
136124 match self {
137- Index :: Uniform { min, max } => Ok ( Uniform :: new ( * min, * max) . sample ( uniform_rng . rng ( ) ) ) ,
125+ Index :: Uniform { min, max } => Ok ( Uniform :: new ( * min, * max) . sample ( & mut * rng) ) ,
138126 Index :: WeightedIndex { weights } => {
139- Ok ( WeightedIndex :: new ( weights. clone ( ) ) ?. sample ( uniform_rng . rng ( ) ) )
127+ Ok ( WeightedIndex :: new ( weights. clone ( ) ) ?. sample ( & mut * rng) )
140128 }
141129 }
142130 }
143131}
144132
145133#[ cfg( test) ]
146134mod tests {
135+ use crate :: input_modeling:: dynamic_rng:: default_rng;
136+
147137 use super :: * ;
148138
149139 enum RandomVariable {
@@ -171,14 +161,14 @@ mod tests {
171161 }
172162
173163 fn empirical_mean ( random_variable : & mut RandomVariable , sample_size : usize ) -> f64 {
174- let mut uniform_rng = UniformRNG :: default ( ) ;
164+ let uniform_rng = default_rng ( ) ;
175165 ( 0 ..sample_size)
176166 . map ( |_| match random_variable {
177167 RandomVariable :: Continuous ( variable) => {
178- variable. random_variate ( & mut uniform_rng) . unwrap ( )
168+ variable. random_variate ( uniform_rng. clone ( ) ) . unwrap ( )
179169 }
180170 RandomVariable :: Discrete ( variable) => {
181- variable. random_variate ( & mut uniform_rng) . unwrap ( ) as f64
171+ variable. random_variate ( uniform_rng. clone ( ) ) . unwrap ( ) as f64
182172 }
183173 } )
184174 . sum :: < f64 > ( )
@@ -187,26 +177,26 @@ mod tests {
187177
188178 fn chi_square ( test : & mut ChiSquareTest , expected_counts : & [ usize ] ) -> f64 {
189179 let mut class_counts = vec ! [ 0 ; expected_counts. len( ) ] ;
190- let mut uniform_rng = UniformRNG :: default ( ) ;
180+ let uniform_rng = default_rng ( ) ;
191181 let sample_size = expected_counts. iter ( ) . sum ( ) ;
192182 ( 0 ..sample_size) . for_each ( |_| {
193183 let index = match test {
194184 ChiSquareTest :: Continuous {
195185 variable,
196186 bin_mapping_fn,
197- } => bin_mapping_fn ( variable. random_variate ( & mut uniform_rng) . unwrap ( ) ) ,
187+ } => bin_mapping_fn ( variable. random_variate ( uniform_rng. clone ( ) ) . unwrap ( ) ) ,
198188 ChiSquareTest :: Boolean {
199189 variable,
200190 bin_mapping_fn,
201- } => bin_mapping_fn ( variable. random_variate ( & mut uniform_rng) . unwrap ( ) ) ,
191+ } => bin_mapping_fn ( variable. random_variate ( uniform_rng. clone ( ) ) . unwrap ( ) ) ,
202192 ChiSquareTest :: Discrete {
203193 variable,
204194 bin_mapping_fn,
205- } => bin_mapping_fn ( variable. random_variate ( & mut uniform_rng) . unwrap ( ) ) ,
195+ } => bin_mapping_fn ( variable. random_variate ( uniform_rng. clone ( ) ) . unwrap ( ) ) ,
206196 ChiSquareTest :: Index {
207197 variable,
208198 bin_mapping_fn,
209- } => bin_mapping_fn ( variable. random_variate ( & mut uniform_rng) . unwrap ( ) ) ,
199+ } => bin_mapping_fn ( variable. random_variate ( uniform_rng. clone ( ) ) . unwrap ( ) ) ,
210200 } ;
211201 class_counts[ index] += 1
212202 } ) ;
0 commit comments