]>
git.proxmox.com Git - rustc.git/blob - vendor/rand-0.7.3/src/distributions/unit_circle.rs
1 // Copyright 2018 Developers of the Rand project.
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.
10 #![allow(clippy::all)]
12 use crate::distributions
::{Distribution, Uniform}
;
15 /// Samples uniformly from the edge of the unit circle in two dimensions.
17 /// Implemented via a method by von Neumann[^1].
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 #[deprecated(since = "0.7.0", note = "moved to rand_distr crate")]
24 #[derive(Clone, Copy, Debug)]
25 pub struct UnitCircle
;
28 /// Construct a new `UnitCircle` distribution.
30 pub fn new() -> UnitCircle
{
35 impl Distribution
<[f64; 2]> for UnitCircle
{
37 fn sample
<R
: Rng
+ ?Sized
>(&self, rng
: &mut R
) -> [f64; 2] {
38 let uniform
= Uniform
::new(-1., 1.);
43 x1
= uniform
.sample(rng
);
44 x2
= uniform
.sample(rng
);
45 sum
= x1
* x1
+ x2
* x2
;
50 let diff
= x1
* x1
- x2
* x2
;
51 [diff
/ sum
, 2. * x1
* x2
/ sum
]
57 use super::UnitCircle
;
58 use crate::distributions
::Distribution
;
60 /// Assert that two numbers are almost equal to each other.
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();
69 "assertion failed: `abs(left - right) = {:.1e} < {:e}`, \
70 (left: `{}`, right: `{}`)",
79 let mut rng
= crate::test
::rng(1);
80 let dist
= UnitCircle
::new();
82 let x
= dist
.sample(&mut rng
);
83 assert_almost_eq
!(x
[0] * x
[0] + x
[1] * x
[1], 1., 1e
-15);
88 fn value_stability() {
89 let mut rng
= crate::test
::rng(2);
91 [-0.9965658683520504, -0.08280380447614634],
92 [-0.9790853270389644, -0.20345004884984505],
93 [-0.8449189758898707, 0.5348943112253227],
96 UnitCircle
.sample(&mut rng
),
97 UnitCircle
.sample(&mut rng
),
98 UnitCircle
.sample(&mut rng
),
100 assert_eq
!(samples
, expected
);