{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang-nursery/rand/issues/58","repository_url":"https://api.github.com/repos/rust-lang-nursery/rand","labels_url":"https://api.github.com/repos/rust-lang-nursery/rand/issues/58/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang-nursery/rand/issues/58/comments","events_url":"https://api.github.com/repos/rust-lang-nursery/rand/issues/58/events","html_url":"https://github.com/rust-lang-nursery/rand/issues/58","id":94943844,"number":58,"title":"`gen_iter()` for distributions?","user":{"login":"JP-Ellis","id":3196162,"avatar_url":"https://avatars1.githubusercontent.com/u/3196162?v=4","gravatar_id":"","url":"https://api.github.com/users/JP-Ellis","html_url":"https://github.com/JP-Ellis","followers_url":"https://api.github.com/users/JP-Ellis/followers","following_url":"https://api.github.com/users/JP-Ellis/following{/other_user}","gists_url":"https://api.github.com/users/JP-Ellis/gists{/gist_id}","starred_url":"https://api.github.com/users/JP-Ellis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JP-Ellis/subscriptions","organizations_url":"https://api.github.com/users/JP-Ellis/orgs","repos_url":"https://api.github.com/users/JP-Ellis/repos","events_url":"https://api.github.com/users/JP-Ellis/events{/privacy}","received_events_url":"https://api.github.com/users/JP-Ellis/received_events","type":"User","site_admin":false},"labels":[{"id":855408180,"url":"https://api.github.com/repos/rust-lang-nursery/rand/labels/E-easy","name":"E-easy","color":"00FF00","default":false},{"id":626813905,"url":"https://api.github.com/repos/rust-lang-nursery/rand/labels/F-new-int","name":"F-new-int","color":"5555ff","default":false},{"id":864763173,"url":"https://api.github.com/repos/rust-lang-nursery/rand/labels/P-medium","name":"P-medium","color":"d46ad4","default":false},{"id":173092856,"url":"https://api.github.com/repos/rust-lang-nursery/rand/labels/X-enhancement","name":"X-enhancement","color":"bfbfbf","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2015-07-14T13:21:18Z","updated_at":"2018-03-30T19:58:16Z","closed_at":null,"author_association":"NONE","body":"The `Rng` trait has the `gen_iter()` function which creates an iterator producing an infinite numbers of random numbers.  It is then easy to manipulate the the iterator and generate vectors of random numbers.\n\nIs there a reason why the various distributions do not implement this?  In my particular case, I am needing to create vectors of numbers distributed normally and there is no easy way of doing that.  One can't use `vec![normal.ind_sample(rng); len];` because it evaluates the function once and then clones the result.\n\nI am quite happy to implement this in this crate (if people are interested).  I was thinking of maybe adding `gen_iter<R: Rng>(&mut self, rng: &mut R)` and `gen_ind_iter<R: Rng>(&mut self, rng: &mut R)` under `Sample` and `IndependentSample` respectively.\n"},"comment":{"url":"https://api.github.com/repos/rust-lang-nursery/rand/issues/comments/377608979","html_url":"https://github.com/rust-lang-nursery/rand/issues/58#issuecomment-377608979","issue_url":"https://api.github.com/repos/rust-lang-nursery/rand/issues/58","id":377608979,"user":{"login":"pitdicker","id":6255050,"avatar_url":"https://avatars1.githubusercontent.com/u/6255050?v=4","gravatar_id":"","url":"https://api.github.com/users/pitdicker","html_url":"https://github.com/pitdicker","followers_url":"https://api.github.com/users/pitdicker/followers","following_url":"https://api.github.com/users/pitdicker/following{/other_user}","gists_url":"https://api.github.com/users/pitdicker/gists{/gist_id}","starred_url":"https://api.github.com/users/pitdicker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pitdicker/subscriptions","organizations_url":"https://api.github.com/users/pitdicker/orgs","repos_url":"https://api.github.com/users/pitdicker/repos","events_url":"https://api.github.com/users/pitdicker/events{/privacy}","received_events_url":"https://api.github.com/users/pitdicker/received_events","type":"User","site_admin":false},"created_at":"2018-03-30T19:58:16Z","updated_at":"2018-03-30T19:58:16Z","author_association":"COLLABORATOR","body":"Would adding an iterator method to the distribution trait like this have disadvantages?\r\n```rust\r\npub trait Distribution<T> {\r\n    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T;\r\n\r\n    fn iter<'r, R: Rng>(self, rng: &'r mut R) -> DistIter<'r, Self, R, T>\r\n        where Self: Sized\r\n    {\r\n        DistIter {\r\n            distr: self,\r\n            rng: rng,\r\n            phantom: ::core::marker::PhantomData,\r\n        }\r\n    }\r\n}\r\n\r\n#[derive(Debug)]\r\npub struct DistIter<'a, D, R, T> where D: Distribution<T>, R: Rng + 'a {\r\n    distr: D,\r\n    rng: &'a mut R,\r\n    phantom: ::core::marker::PhantomData<T>,\r\n}\r\n\r\nimpl<'a, D, R, T> Iterator for DistIter<'a, D, R, T>\r\n    where D: Distribution<T>, R: Rng + 'a\r\n{\r\n    type Item = T;\r\n\r\n    fn next(&mut self) -> Option<T> {\r\n        Some(self.rng.sample(&self.distr))\r\n    }\r\n}\r\n```\r\n\r\nIt can be used like this:\r\n```rust\r\nuse distributions::Normal;\r\nlet mut rng = thread_rng();\r\nlet distr = Normal::new(10.0, 10.0);\r\nlet results: Vec<_> = distr.iter(&mut rng).take(100).collect();\r\n\r\n```"}}
{"actor":{"display_login":"pitdicker"}}