Skip to content

Commit af25e61

Browse files
yogesumbudziq
authored andcommitted
fix rand crate example and broken links
1 parent 78a59fb commit af25e61

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

CONTRIBUTING.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,17 @@ after the code sample.
162162
> By default, random numbers are generated with [uniform distribution].
163163
> To generate numbers with other distributions you instantiate a
164164
> distribution, then sample from that distribution using
165-
> [`IndependentSample::ind_sample`] with help of a random-number
165+
> [`Distribution::sample`] with help of a random-number
166166
> generator [`rand::Rng`].
167167
>
168168
> The [distributions available are documented here][rand-distributions].
169169
> An example using the [`Normal`] distribution is shown below.
170170
171171
[uniform distribution]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)
172-
[`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample
173-
[`rand::Rng`]: https://doc.rust-lang.org/rand/rand/trait.Rng.html
174-
[rand-distributions]: https://doc.rust-lang.org/rand/rand/distributions/index.html
175-
[`Normal`]: https://doc.rust-lang.org/rand/rand/distributions/normal/struct.Normal.html
172+
[`Distribution::sample`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html#tymethod.sample
173+
[`rand::Rng`]: https://docs.rs/rand/*/rand/trait.Rng.html
174+
[rand-distributions]: https://docs.rs/rand/*/rand/distributions/index.html
175+
[`Normal`]: https://docs.rs/rand/*/rand/distributions/struct.Normal.html
176176

177177
#### Code
178178

@@ -207,12 +207,12 @@ explanation in the description.
207207
> ```rust
208208
> extern crate rand;
209209
>
210-
> use rand::distributions::{Normal, IndependentSample};
210+
> use rand::distributions::{Normal, Distribution};
211211
>
212212
> fn main() {
213213
> let mut rng = rand::thread_rng();
214214
> let normal = Normal::new(2.0, 3.0);
215-
> let v = normal.ind_sample(&mut rng);
215+
> let v = normal.sample(&mut rng);
216216
> println!("{} is from a N(2, 9) distribution", v)
217217
> }
218218
> ```

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mime = "0.3"
3131
num = "0.1"
3232
num_cpus = "1.8"
3333
petgraph = "0.4"
34-
rand = "0.4"
34+
rand = "0.5"
3535
rayon = "1.0"
3636
regex = "1.0"
3737
reqwest = "0.8"

src/basics.md

+22-16
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ in the same range.
194194
```rust
195195
extern crate rand;
196196

197-
use rand::distributions::{Range, IndependentSample};
197+
use rand::distributions::{Range, Distribution};
198198

199199
fn main() {
200200
let mut rng = rand::thread_rng();
201201
let die = Range::new(1, 7);
202202

203203
loop {
204-
let throw = die.ind_sample(&mut rng);
204+
let throw = die.sample(&mut rng);
205205
println!("Roll the die: {}", throw);
206206
if throw == 6 {
207207
break;
@@ -221,7 +221,7 @@ fn main() {
221221
By default, random numbers are generated with [uniform distribution].
222222
To generate numbers with other distributions you instantiate a
223223
distribution, then sample from that distribution using
224-
[`IndependentSample::ind_sample`] with help of a random-number
224+
[`Distribution::sample`] with help of a random-number
225225
generator [`rand::Rng`].
226226

227227
The [distributions available are documented here][rand-distributions]. An example using the
@@ -230,14 +230,14 @@ The [distributions available are documented here][rand-distributions]. An exampl
230230
```rust
231231
extern crate rand;
232232

233-
use rand::distributions::{Normal, IndependentSample};
233+
use rand::distributions::{Normal, Distribution};
234234

235235
fn main() {
236236
let mut rng = rand::thread_rng();
237237

238238
// mean 2, standard deviation 3:
239239
let normal = Normal::new(2.0, 3.0);
240-
let v = normal.ind_sample(&mut rng);
240+
let v = normal.sample(&mut rng);
241241
println!("{} is from a N(2, 9) distribution", v)
242242
}
243243
```
@@ -249,21 +249,22 @@ fn main() {
249249
[![rand-badge]][rand] [![cat-science-badge]][cat-science]
250250

251251
Randomly generates a tuple `(i32, bool, f64)` and variable of user defined type `Point`.
252-
Implements the [`rand::Rand`] trait for `Point` in order to allow random generation.
252+
Implements the [`Distribution`] trait on type `Point` for [`Standard`] in order to allow random generation.
253253

254254
```rust
255255
extern crate rand;
256256

257-
use rand::{Rng, Rand};
257+
use rand::Rng;
258+
use rand::distributions::{Distribution, Standard};
258259

259260
#[derive(Debug)]
260261
struct Point {
261262
x: i32,
262263
y: i32,
263264
}
264265

265-
impl Rand for Point {
266-
fn rand<R: Rng>(rng: &mut R) -> Point {
266+
impl Distribution<Point> for Standard {
267+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Point {
267268
let (rand_x, rand_y) = rng.gen();
268269
Point {
269270
x: rand_x,
@@ -287,15 +288,20 @@ fn main() {
287288

288289
[![rand-badge]][rand] [![cat-os-badge]][cat-os]
289290

290-
Randomly generates a string of given length ASCII characters in the range `A-Z, a-z, 0-9`, with [`gen_ascii_chars`].
291+
Randomly generates a string of given length ASCII characters in the range `A-Z, a-z, 0-9`, with [`Alphanumeric`] sample.
291292

292293
```rust
293294
extern crate rand;
294295

295296
use rand::{thread_rng, Rng};
297+
use rand::distributions::Alphanumeric;
296298

297299
fn main() {
298-
let rand_string: String = thread_rng().gen_ascii_chars().take(30).collect();
300+
let rand_string: String = thread_rng()
301+
.sample_iter(&Alphanumeric)
302+
.take(30)
303+
.collect();
304+
299305
println!("{}", rand_string);
300306
}
301307
```
@@ -1806,10 +1812,10 @@ fn run() -> Result<()> {
18061812
[`foreign_links`]: https://docs.rs/error-chain/*/error_chain/#foreign-links
18071813
[`fs::Metadata`]: https://doc.rust-lang.org/std/fs/struct.Metadata.html
18081814
[`fs::read_dir`]: https://doc.rust-lang.org/std/fs/fn.read_dir.html
1809-
[`gen_ascii_chars`]: https://docs.rs/rand/*/rand/trait.Rng.html#method.gen_ascii_chars
1815+
[`Alphanumeric`]: https://docs.rs/rand/*/rand/distributions/struct.Alphanumeric.html#struct.Alphanumaric
18101816
[`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
18111817
[`hmac::Signature`]: https://briansmith.org/rustdoc/ring/hmac/struct.Signature.html
1812-
[`IndependentSample::ind_sample`]: https://docs.rs/rand/*/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample
1818+
[`Distribution::sample`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html#tymethod.sample
18131819
[`Lines`]: https://doc.rust-lang.org/std/io/struct.Lines.html
18141820
[`Metadata::is_file`]: https://doc.rust-lang.org/std/fs/struct.Metadata.html#method.is_file
18151821
[`Metadata::modified`]: https://doc.rust-lang.org/std/fs/struct.Metadata.html#method.modified
@@ -1831,11 +1837,11 @@ fn run() -> Result<()> {
18311837
[`pbkdf2::derive`]: https://briansmith.org/rustdoc/ring/pbkdf2/fn.derive.html
18321838
[`pbkdf2::verify`]: https://briansmith.org/rustdoc/ring/pbkdf2/fn.verify.html
18331839
[`process::Stdio`]: https://doc.rust-lang.org/std/process/struct.Stdio.html
1834-
[`rand::Rand`]: https://docs.rs/rand/*/rand/trait.Rand.html
1835-
[`rand::Rand`]: https://docs.rs/rand/*/rand/trait.Rand.html
18361840
[`rand::Rng`]: https://docs.rs/rand/*/rand/trait.Rng.html
18371841
[`rand::thread_rng`]: https://docs.rs/rand/*/rand/fn.thread_rng.html
1838-
[`Range`]: https://docs.rs/rand/*/rand/distributions/range/struct.Range.html
1842+
[`Range`]: https://docs.rs/rand/*/rand/distributions/#reexports
1843+
[`Standard`]: https://docs.rs/rand/*/rand/distributions/struct.Standard.html
1844+
[`Distribution`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html
18391845
[`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
18401846
[`Regex::captures_iter`]: https://doc.rust-lang.org/regex/*/regex/struct.Regex.html#method.captures_iter
18411847
[`regex::RegexSet`]: https://doc.rust-lang.org/regex/*/regex/struct.RegexSet.html

src/concurrency.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ because it is usually faster than [`stable sorting`] algorithms which `rayon` al
8787
extern crate rand;
8888
extern crate rayon;
8989

90-
use rand::Rng;
90+
use rand::{Rng, thread_rng};
91+
use rand::distributions::Alphanumeric;
9192
use rayon::prelude::*;
9293

9394
fn main() {
@@ -97,7 +98,8 @@ fn main() {
9798
// [2]
9899
vec.par_iter_mut().for_each(|p| {
99100
// [3]
100-
*p = rand::weak_rng().gen_ascii_chars().take(5).collect()
101+
let mut rng = thread_rng();
102+
*p = (0..5).map(|_| rng.sample(&Alphanumeric)).collect()
101103
});
102104

103105
// [4]

0 commit comments

Comments
 (0)