Skip to content

Commit 6f9b59a

Browse files
committed
---
yaml --- r: 94138 b: refs/heads/try c: 1d986de h: refs/heads/master v: v3
1 parent 062e3eb commit 6f9b59a

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0da105a8b7b6b1e0568e8ff20f6ff4b13cc7ecc2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d3e57dca68fde4effdda3e4ae2887aa535fcd6
5-
refs/heads/try: 9d5639d11ba5d3ef4ca6be323d6604a5b9e0caaa
5+
refs/heads/try: 1d986de2488249763f730ba83fd3d8235391e74d
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/rand/distributions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use clone::Clone;
2828

2929
pub use self::range::Range;
3030
pub use self::gamma::Gamma;
31-
pub use self::normal::Normal;
31+
pub use self::normal::{Normal, LogNormal};
3232
pub use self::exponential::Exp;
3333

3434
pub mod range;

branches/try/src/libstd/rand/distributions/normal.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! The normal distribution.
11+
//! The normal and derived distributions.
1212
1313
use rand::{Rng, Rand, Open01};
1414
use rand::distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
@@ -108,6 +108,46 @@ impl IndependentSample<f64> for Normal {
108108
}
109109
}
110110

111+
112+
/// The log-normal distribution `ln N(mean, std_dev**2)`.
113+
///
114+
/// If `X` is log-normal distributed, then `ln(X)` is `N(mean,
115+
/// std_dev**2)` distributed.
116+
///
117+
/// # Example
118+
///
119+
/// ```rust
120+
/// use std::rand;
121+
/// use std::rand::distributions::{LogNormal, IndependentSample};
122+
///
123+
/// fn main() {
124+
/// // mean 2, standard deviation 3
125+
/// let log_normal = LogNormal::new(2.0, 3.0);
126+
/// let v = normal.ind_sample(&mut rand::task_rng());
127+
/// println!("{} is from an ln N(2, 9) distribution", v)
128+
/// }
129+
/// ```
130+
pub struct LogNormal {
131+
priv norm: Normal
132+
}
133+
134+
impl LogNormal {
135+
/// Construct a new `LogNormal` distribution with the given mean
136+
/// and standard deviation. Fails if `std_dev < 0`.
137+
pub fn new(mean: f64, std_dev: f64) -> LogNormal {
138+
assert!(std_dev >= 0.0, "LogNormal::new called with `std_dev` < 0");
139+
LogNormal { norm: Normal::new(mean, std_dev) }
140+
}
141+
}
142+
impl Sample<f64> for LogNormal {
143+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
144+
}
145+
impl IndependentSample<f64> for LogNormal {
146+
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
147+
self.norm.ind_sample(rng).exp()
148+
}
149+
}
150+
111151
#[cfg(test)]
112152
mod tests {
113153
use rand::*;
@@ -129,6 +169,22 @@ mod tests {
129169
fn test_normal_invalid_sd() {
130170
Normal::new(10.0, -1.0);
131171
}
172+
173+
174+
#[test]
175+
fn test_log_normal() {
176+
let mut lnorm = LogNormal::new(10.0, 10.0);
177+
let mut rng = task_rng();
178+
for _ in range(0, 1000) {
179+
lnorm.sample(&mut rng);
180+
lnorm.ind_sample(&mut rng);
181+
}
182+
}
183+
#[test]
184+
#[should_fail]
185+
fn test_log_normal_invalid_sd() {
186+
LogNormal::new(10.0, -1.0);
187+
}
132188
}
133189

134190
#[cfg(test)]

0 commit comments

Comments
 (0)