-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an iterator to Distribution
#361
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,33 +299,6 @@ pub trait Rand : Sized { | |
/// } | ||
/// ``` | ||
/// | ||
/// # Iteration | ||
/// | ||
/// Iteration over an `Rng` can be achieved using `iter::repeat` as follows: | ||
/// | ||
/// ```rust | ||
/// use std::iter; | ||
/// use rand::{Rng, thread_rng}; | ||
/// use rand::distributions::{Alphanumeric, Range}; | ||
/// | ||
/// let mut rng = thread_rng(); | ||
/// | ||
/// // Vec of 16 x f32: | ||
/// let v: Vec<f32> = iter::repeat(()).map(|()| rng.gen()).take(16).collect(); | ||
/// | ||
/// // String: | ||
/// let s: String = iter::repeat(()) | ||
/// .map(|()| rng.sample(Alphanumeric)) | ||
/// .take(7).collect(); | ||
/// | ||
/// // Dice-rolling: | ||
/// let die_range = Range::new_inclusive(1, 6); | ||
/// let mut roll_die = iter::repeat(()).map(|()| rng.sample(die_range)); | ||
/// while roll_die.next().unwrap() != 6 { | ||
/// println!("Not a 6; rolling again!"); | ||
/// } | ||
/// ``` | ||
/// | ||
/// [`RngCore`]: https://docs.rs/rand_core/0.1/rand_core/trait.RngCore.html | ||
pub trait Rng: RngCore { | ||
/// Fill `dest` entirely with random bytes (uniform value distribution), | ||
|
@@ -407,6 +380,39 @@ pub trait Rng: RngCore { | |
fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T { | ||
distr.sample(self) | ||
} | ||
|
||
/// Create an iterator that generates values using the given distribution. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use rand::{thread_rng, Rng}; | ||
/// use rand::distributions::{Alphanumeric, Range, Uniform}; | ||
/// | ||
/// let mut rng = thread_rng(); | ||
/// | ||
/// // Vec of 16 x f32: | ||
/// let v: Vec<f32> = thread_rng().sample_iter(&Uniform).take(16).collect(); | ||
/// | ||
/// // String: | ||
/// let s: String = rng.sample_iter(&Alphanumeric).take(7).collect(); | ||
/// | ||
/// // Combined values | ||
/// println!("{:?}", thread_rng().sample_iter(&Uniform).take(5) | ||
/// .collect::<Vec<(f64, bool)>>()); | ||
/// | ||
/// // Dice-rolling: | ||
/// let die_range = Range::new_inclusive(1, 6); | ||
/// let mut roll_die = rng.sample_iter(&die_range); | ||
/// while roll_die.next().unwrap() != 6 { | ||
/// println!("Not a 6; rolling again!"); | ||
/// } | ||
/// ``` | ||
fn sample_iter<'a, T, D: Distribution<T>>(&'a mut self, distr: &'a D) | ||
-> distributions::DistIter<'a, D, Self, T> where Self: Sized | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting that this takes the distribution by reference while There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we can have both, because the iterator expects an reference instead of something owned. Or do we want to use some sort of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will work if you just change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, of course the iterator is returned from |
||
{ | ||
distr.sample_iter(self) | ||
} | ||
|
||
/// Return a random value supporting the [`Uniform`] distribution. | ||
/// | ||
|
@@ -442,7 +448,7 @@ pub trait Rng: RngCore { | |
/// .collect::<Vec<(f64, bool)>>()); | ||
/// ``` | ||
#[allow(deprecated)] | ||
#[deprecated(since="0.5.0", note="use iter::repeat instead")] | ||
#[deprecated(since="0.5.0", note="use Rng::sample_iter(&Uniform) instead")] | ||
fn gen_iter<T>(&mut self) -> Generator<T, &mut Self> where Uniform: Distribution<T> { | ||
Generator { rng: self, _marker: marker::PhantomData } | ||
} | ||
|
@@ -527,7 +533,7 @@ pub trait Rng: RngCore { | |
/// println!("{}", s); | ||
/// ``` | ||
#[allow(deprecated)] | ||
#[deprecated(since="0.5.0", note="use distributions::Alphanumeric instead")] | ||
#[deprecated(since="0.5.0", note="use sample_iter(&Alphanumeric) instead")] | ||
fn gen_ascii_chars(&mut self) -> AsciiGenerator<&mut Self> { | ||
AsciiGenerator { rng: self } | ||
} | ||
|
@@ -678,7 +684,7 @@ impl_as_byte_slice_arrays!(32, N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N | |
/// [`Rng`]: trait.Rng.html | ||
#[derive(Debug)] | ||
#[allow(deprecated)] | ||
#[deprecated(since="0.5.0", note="use iter::repeat instead")] | ||
#[deprecated(since="0.5.0", note="use Rng::sample_iter instead")] | ||
pub struct Generator<T, R: RngCore> { | ||
rng: R, | ||
_marker: marker::PhantomData<fn() -> T>, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, but lets not promote bad styles in examples by creating a local
rng
handle then not using it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to show that it was also possible to use
thread_rng()
directly. But yes, bad style. Will change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think most people will figure that out.