Skip to content

Commit 81ad512

Browse files
committed
Update formatting
1 parent c0de3b0 commit 81ad512

23 files changed

+125
-121
lines changed

examples/monty-hall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
//!
2727
//! [Monty Hall Problem]: https://en.wikipedia.org/wiki/Monty_Hall_problem
2828
29-
use rand::distr::{Distribution, Uniform};
3029
use rand::Rng;
30+
use rand::distr::{Distribution, Uniform};
3131

3232
struct SimulationResult {
3333
win: bool,

examples/rayon-monte-carlo.rs

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
1-
// Copyright 2018 Developers of the Rand project.
2-
// Copyright 2013-2018 The Rust Project Developers.
3-
//
4-
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5-
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6-
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7-
// option. This file may not be copied, modified, or distributed
8-
// except according to those terms.
9-
10-
//! # Monte Carlo estimation of π with a chosen seed and rayon for parallelism
11-
//!
12-
//! Imagine that we have a square with sides of length 2 and a unit circle
13-
//! (radius = 1), both centered at the origin. The areas are:
14-
//!
15-
//! ```text
16-
//! area of circle = πr² = π * r * r = π
17-
//! area of square = 2² = 4
18-
//! ```
19-
//!
20-
//! The circle is entirely within the square, so if we sample many points
21-
//! randomly from the square, roughly π / 4 of them should be inside the circle.
22-
//!
23-
//! We can use the above fact to estimate the value of π: pick many points in
24-
//! the square at random, calculate the fraction that fall within the circle,
25-
//! and multiply this fraction by 4.
26-
//!
27-
//! Note on determinism:
28-
//! It's slightly tricky to build a parallel simulation using Rayon
29-
//! which is both efficient *and* reproducible.
30-
//!
31-
//! Rayon's ParallelIterator api does not guarantee that the work will be
32-
//! batched into identical batches on every run, so we can't simply use
33-
//! map_init to construct one RNG per Rayon batch.
34-
//!
35-
//! Instead, we do our own batching, so that a Rayon work item becomes a
36-
//! batch. Then we can fix our rng stream to the batched work item.
37-
//! Batching amortizes the cost of constructing the Rng from a fixed seed
38-
//! over BATCH_SIZE trials. Manually batching also turns out to be faster
39-
//! for the nondeterministic version of this program as well.
40-
41-
use rand::distr::{Distribution, Uniform};
42-
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
43-
use rayon::prelude::*;
44-
45-
static SEED: u64 = 0;
46-
static BATCH_SIZE: u64 = 10_000;
47-
static BATCHES: u64 = 1000;
48-
49-
fn main() {
50-
let range = Uniform::new(-1.0f64, 1.0).unwrap();
51-
52-
let in_circle = (0..BATCHES)
53-
.into_par_iter()
54-
.map(|i| {
55-
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
56-
// We chose ChaCha because it's fast, has suitable statistical properties for simulation,
57-
// and because it supports this set_stream() api, which lets us choose a different stream
58-
// per work item. ChaCha supports 2^64 independent streams.
59-
rng.set_stream(i);
60-
let mut count = 0;
61-
for _ in 0..BATCH_SIZE {
62-
let a = range.sample(&mut rng);
63-
let b = range.sample(&mut rng);
64-
if a * a + b * b <= 1.0 {
65-
count += 1;
66-
}
67-
}
68-
count
69-
})
70-
.sum::<usize>();
71-
72-
// assert this is deterministic
73-
assert_eq!(in_circle, 7852263);
74-
75-
// prints something close to 3.14159...
76-
println!(
77-
"π is approximately {}",
78-
4. * (in_circle as f64) / ((BATCH_SIZE * BATCHES) as f64)
79-
);
80-
}
1+
// Copyright 2018 Developers of the Rand project.
2+
// Copyright 2013-2018 The Rust Project Developers.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
//! # Monte Carlo estimation of π with a chosen seed and rayon for parallelism
11+
//!
12+
//! Imagine that we have a square with sides of length 2 and a unit circle
13+
//! (radius = 1), both centered at the origin. The areas are:
14+
//!
15+
//! ```text
16+
//! area of circle = πr² = π * r * r = π
17+
//! area of square = 2² = 4
18+
//! ```
19+
//!
20+
//! The circle is entirely within the square, so if we sample many points
21+
//! randomly from the square, roughly π / 4 of them should be inside the circle.
22+
//!
23+
//! We can use the above fact to estimate the value of π: pick many points in
24+
//! the square at random, calculate the fraction that fall within the circle,
25+
//! and multiply this fraction by 4.
26+
//!
27+
//! Note on determinism:
28+
//! It's slightly tricky to build a parallel simulation using Rayon
29+
//! which is both efficient *and* reproducible.
30+
//!
31+
//! Rayon's ParallelIterator api does not guarantee that the work will be
32+
//! batched into identical batches on every run, so we can't simply use
33+
//! map_init to construct one RNG per Rayon batch.
34+
//!
35+
//! Instead, we do our own batching, so that a Rayon work item becomes a
36+
//! batch. Then we can fix our rng stream to the batched work item.
37+
//! Batching amortizes the cost of constructing the Rng from a fixed seed
38+
//! over BATCH_SIZE trials. Manually batching also turns out to be faster
39+
//! for the nondeterministic version of this program as well.
40+
41+
use rand::distr::{Distribution, Uniform};
42+
use rand_chacha::{ChaCha8Rng, rand_core::SeedableRng};
43+
use rayon::prelude::*;
44+
45+
static SEED: u64 = 0;
46+
static BATCH_SIZE: u64 = 10_000;
47+
static BATCHES: u64 = 1000;
48+
49+
fn main() {
50+
let range = Uniform::new(-1.0f64, 1.0).unwrap();
51+
52+
let in_circle = (0..BATCHES)
53+
.into_par_iter()
54+
.map(|i| {
55+
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
56+
// We chose ChaCha because it's fast, has suitable statistical properties for simulation,
57+
// and because it supports this set_stream() api, which lets us choose a different stream
58+
// per work item. ChaCha supports 2^64 independent streams.
59+
rng.set_stream(i);
60+
let mut count = 0;
61+
for _ in 0..BATCH_SIZE {
62+
let a = range.sample(&mut rng);
63+
let b = range.sample(&mut rng);
64+
if a * a + b * b <= 1.0 {
65+
count += 1;
66+
}
67+
}
68+
count
69+
})
70+
.sum::<usize>();
71+
72+
// assert this is deterministic
73+
assert_eq!(in_circle, 7852263);
74+
75+
// prints something close to 3.14159...
76+
println!(
77+
"π is approximately {}",
78+
4. * (in_circle as f64) / ((BATCH_SIZE * BATCHES) as f64)
79+
);
80+
}

rand_core/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ pub struct RngReadAdapter<'a, R: TryRngCore + ?Sized> {
597597
impl<R: TryRngCore + ?Sized> std::io::Read for RngReadAdapter<'_, R> {
598598
#[inline]
599599
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
600-
self.inner.try_fill_bytes(buf).map_err(|err| {
601-
std::io::Error::other(std::format!("RNG error: {err}"))
602-
})?;
600+
self.inner
601+
.try_fill_bytes(buf)
602+
.map_err(|err| std::io::Error::other(std::format!("RNG error: {err}")))?;
603603
Ok(buf.len())
604604
}
605605
}

rand_pcg/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ mod pcg64;
9595

9696
pub use rand_core;
9797

98+
pub use self::pcg64::{Lcg64Xsh32, Pcg32};
9899
pub use self::pcg128::{Lcg128Xsl64, Mcg128Xsl64, Pcg64, Pcg64Mcg};
99100
pub use self::pcg128cm::{Lcg128CmDxsm64, Pcg64Dxsm};
100-
pub use self::pcg64::{Lcg64Xsh32, Pcg32};

rand_pcg/src/pcg128.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
const MULTIPLIER: u128 = 0x2360_ED05_1FC6_5DA4_4385_DF64_9FCC_F645;
1515

1616
use core::fmt;
17-
use rand_core::{impls, le, RngCore, SeedableRng};
17+
use rand_core::{RngCore, SeedableRng, impls, le};
1818
#[cfg(feature = "serde")]
1919
use serde::{Deserialize, Serialize};
2020

rand_pcg/src/pcg128cm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
const MULTIPLIER: u64 = 15750249268501108917;
1515

1616
use core::fmt;
17-
use rand_core::{impls, le, RngCore, SeedableRng};
17+
use rand_core::{RngCore, SeedableRng, impls, le};
1818
#[cfg(feature = "serde")]
1919
use serde::{Deserialize, Serialize};
2020

rand_pcg/src/pcg64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! PCG random number generators
1212
1313
use core::fmt;
14-
use rand_core::{impls, le, RngCore, SeedableRng};
14+
use rand_core::{RngCore, SeedableRng, impls, le};
1515
#[cfg(feature = "serde")]
1616
use serde::{Deserialize, Serialize};
1717

src/distr/bernoulli.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
//! The Bernoulli distribution `Bernoulli(p)`.
1010
11-
use crate::distr::Distribution;
1211
use crate::Rng;
12+
use crate::distr::Distribution;
1313
use core::fmt;
1414

1515
#[cfg(feature = "serde")]
@@ -165,8 +165,8 @@ impl Distribution<bool> for Bernoulli {
165165
#[cfg(test)]
166166
mod test {
167167
use super::Bernoulli;
168-
use crate::distr::Distribution;
169168
use crate::Rng;
169+
use crate::distr::Distribution;
170170

171171
#[test]
172172
#[cfg(feature = "serde")]
@@ -232,7 +232,9 @@ mod test {
232232
}
233233
assert_eq!(
234234
buf,
235-
[true, false, false, true, false, false, true, true, true, true]
235+
[
236+
true, false, false, true, false, false, true, true, true, true
237+
]
236238
);
237239
}
238240

src/distr/distribution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ pub trait SampleString {
203203

204204
#[cfg(test)]
205205
mod tests {
206-
use crate::distr::{Distribution, Uniform};
207206
use crate::Rng;
207+
use crate::distr::{Distribution, Uniform};
208208

209209
#[test]
210210
fn test_distributions_iter() {

src/distr/float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
//! Basic floating-point number distributions
1010
11+
use crate::Rng;
1112
use crate::distr::utils::{FloatAsSIMD, FloatSIMDUtils, IntAsSIMD};
1213
use crate::distr::{Distribution, StandardUniform};
13-
use crate::Rng;
1414
use core::mem;
1515
#[cfg(feature = "simd_support")]
1616
use core::simd::prelude::*;

0 commit comments

Comments
 (0)