]> git.proxmox.com Git - rustc.git/blob - vendor/rand-0.7.3/src/distributions/weibull.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / vendor / rand-0.7.3 / src / distributions / weibull.rs
1 // Copyright 2018 Developers of the Rand project.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! The Weibull distribution.
10 #![allow(deprecated)]
11
12 use crate::distributions::{Distribution, OpenClosed01};
13 use crate::Rng;
14
15 /// Samples floating-point numbers according to the Weibull distribution
16 #[deprecated(since = "0.7.0", note = "moved to rand_distr crate")]
17 #[derive(Clone, Copy, Debug)]
18 pub struct Weibull {
19 inv_shape: f64,
20 scale: f64,
21 }
22
23 impl Weibull {
24 /// Construct a new `Weibull` distribution with given `scale` and `shape`.
25 ///
26 /// # Panics
27 ///
28 /// `scale` and `shape` have to be non-zero and positive.
29 pub fn new(scale: f64, shape: f64) -> Weibull {
30 assert!((scale > 0.) & (shape > 0.));
31 Weibull {
32 inv_shape: 1. / shape,
33 scale,
34 }
35 }
36 }
37
38 impl Distribution<f64> for Weibull {
39 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
40 let x: f64 = rng.sample(OpenClosed01);
41 self.scale * (-x.ln()).powf(self.inv_shape)
42 }
43 }
44
45 #[cfg(test)]
46 mod tests {
47 use super::Weibull;
48 use crate::distributions::Distribution;
49
50 #[test]
51 #[should_panic]
52 fn invalid() {
53 Weibull::new(0., 0.);
54 }
55
56 #[test]
57 fn sample() {
58 let scale = 1.0;
59 let shape = 2.0;
60 let d = Weibull::new(scale, shape);
61 let mut rng = crate::test::rng(1);
62 for _ in 0..1000 {
63 let r = d.sample(&mut rng);
64 assert!(r >= 0.);
65 }
66 }
67 }