|
| 1 | +use Rng; |
| 2 | +use distributions::{Distribution, Uniform}; |
| 3 | + |
| 4 | +/// Samples uniformly from the edge of the unit circle in two dimensions. |
| 5 | +/// |
| 6 | +/// Implemented via a method by von Neumann[^1]. |
| 7 | +/// |
| 8 | +/// |
| 9 | +/// # Example |
| 10 | +/// |
| 11 | +/// ``` |
| 12 | +/// use rand::distributions::{UnitCircle, Distribution}; |
| 13 | +/// |
| 14 | +/// let circle = UnitCircle::new(); |
| 15 | +/// let v = circle.sample(&mut rand::thread_rng()); |
| 16 | +/// println!("{:?} is from the unit circle.", v) |
| 17 | +/// ``` |
| 18 | +/// |
| 19 | +/// [^1]: von Neumann, J. (1951) [*Various Techniques Used in Connection with |
| 20 | +/// Random Digits.*](https://mcnp.lanl.gov/pdf_files/nbs_vonneumann.pdf) |
| 21 | +/// NBS Appl. Math. Ser., No. 12. Washington, DC: U.S. Government Printing |
| 22 | +/// Office, pp. 36-38. |
| 23 | +#[derive(Clone, Copy, Debug)] |
| 24 | +pub struct UnitCircle { |
| 25 | + uniform: Uniform<f64>, |
| 26 | +} |
| 27 | + |
| 28 | +impl UnitCircle { |
| 29 | + /// Construct a new `UnitCircle` distribution. |
| 30 | + #[inline] |
| 31 | + pub fn new() -> UnitCircle { |
| 32 | + UnitCircle { uniform: Uniform::new(-1., 1.) } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl Distribution<[f64; 2]> for UnitCircle { |
| 37 | + #[inline] |
| 38 | + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [f64; 2] { |
| 39 | + let mut x1; |
| 40 | + let mut x2; |
| 41 | + let mut sum; |
| 42 | + loop { |
| 43 | + x1 = self.uniform.sample(rng); |
| 44 | + x2 = self.uniform.sample(rng); |
| 45 | + sum = x1*x1 + x2*x2; |
| 46 | + if sum < 1. { |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + let diff = x1*x1 - x2*x2; |
| 51 | + [diff / sum, 2.*x1*x2 / sum] |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use distributions::Distribution; |
| 58 | + use super::UnitCircle; |
| 59 | + |
| 60 | + /// Assert that two numbers are almost equal to each other. |
| 61 | + /// |
| 62 | + /// On panic, this macro will print the values of the expressions with their |
| 63 | + /// debug representations. |
| 64 | + macro_rules! assert_almost_eq { |
| 65 | + ($a:expr, $b:expr, $prec:expr) => ( |
| 66 | + let diff = ($a - $b).abs(); |
| 67 | + if diff > $prec { |
| 68 | + panic!(format!( |
| 69 | + "assertion failed: `abs(left - right) = {:.1e} < {:e}`, \ |
| 70 | + (left: `{}`, right: `{}`)", |
| 71 | + diff, $prec, $a, $b)); |
| 72 | + } |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn norm() { |
| 78 | + let mut rng = ::test::rng(1); |
| 79 | + let dist = UnitCircle::new(); |
| 80 | + for _ in 0..1000 { |
| 81 | + let x = dist.sample(&mut rng); |
| 82 | + assert_almost_eq!(x[0]*x[0] + x[1]*x[1], 1., 1e-15); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn value_stability() { |
| 88 | + let mut rng = ::test::rng(2); |
| 89 | + let dist = UnitCircle::new(); |
| 90 | + assert_eq!(dist.sample(&mut rng), [-0.8150602311723979, 0.5793762331690843]); |
| 91 | + assert_eq!(dist.sample(&mut rng), [-0.056204569973983196, 0.998419273809375]); |
| 92 | + assert_eq!(dist.sample(&mut rng), [0.7761923749562624, -0.630496151502733]); |
| 93 | + } |
| 94 | +} |
0 commit comments