13
13
//! To quickly get you started, the easiest and most high-level way to just get
14
14
//! some random value is to use [`random()`].
15
15
//!
16
- //! ```rust
16
+ //! ```
17
17
//! let x: u8 = rand::random();
18
18
//! println!("{}", x);
19
19
//!
20
20
//! let y = rand::random::<f64>();
21
21
//! println!("{}", y);
22
22
//!
23
23
//! if rand::random() { // generates a boolean
24
- //! println!("Better lucky than good !");
24
+ //! println!("Heads !");
25
25
//! }
26
26
//! ```
27
27
//!
28
28
//! This functionality is however very basic. As soon as you need something
29
29
//! slightly more specific, like a random value in some range, you have to know
30
- //! a bit more about how things work, which are discussed below.
30
+ //! a bit more about how things work, which will be discussed below.
31
31
//!
32
32
//! ## Examples
33
33
//!
45
45
//!
46
46
//! - a random number generator (RNG);
47
47
//! - some function to transform the 'bag of bits' from the RNG to a value you
48
- //! can use.
48
+ //! can use (which we call a distribution, although we use the term for a bit
49
+ //! more functionality than what fits the meaning).
49
50
//!
50
51
//! There are many kinds of RNGs, with different trade-offs. Rand comes with a
51
- //! default, [`thread_rng`]. The numbers it generates are suitable to use in all
52
- //! situations .
52
+ //! good all-use default, [`thread_rng`]. It is reasonably fast and has good
53
+ //! quality (secure and without patterns) .
53
54
//!
54
55
//! To turn the output of the RNG into something usable, you usually want to use
55
56
//! the methods from the [`Rng`] trait:
56
57
//!
57
- //! - [`gen`] generates a random value that fits the type.
58
- //! For example `let val: u16 = rng.gen()` will just fill an `u16` with 16
59
- //! random bits. But for floats the value will be generated between 0 and 1;
60
- //! which is more usable than any value between 0 and infinity.
58
+ //! - [`gen`] generates a random value appropriate for the type.
59
+ //! For example `let val: u16 = rng.gen()` will produces a random value
60
+ //! between 0 and `std::u16::MAX`. But for floats the value will be generated
61
+ //! uniformly between 0 and 1, which is more useful than a completely random
62
+ //! float value (including NaN and infinity);
61
63
//! - [`gen_range`] samples from a specific range of values;
62
64
//! - [`sample`], for all situations where you need something slightly more
63
65
//! complex.
66
68
//!
67
69
//! ### Examples
68
70
//!
69
- //! ```rust
71
+ //! ```
70
72
//! // Rng is the main trait and needs to be imported:
71
73
//! use rand::{Rng, thread_rng};
72
74
//!
88
90
//! impossible, but hard and slow. Generally the operating systems collects some
89
91
//! of them, by measuring different kinds of noice/jitter in the hardware.
90
92
//!
91
- //! What is always used instead is a psuedo-random number generator. This is a
92
- //! clever algorithm that can produce an infinite stream of random numbers from
93
- //! a small seed. There exist algorithms that produce random numbers that are in
94
- //! no measurable way any less random than pulling values 'out of thin air'.
93
+ //! What is always used instead is a psuedo-random number generator (PRNG). This
94
+ //! is a clever algorithm that can produce an infinite stream of psuedo-random
95
+ //! numbers from a small seed. There exist algorithms that produce random
96
+ //! numbers that are in no practically measurable way any less random than
97
+ //! pulling values 'out of thin air'. Except that the random number stream can
98
+ //! be reproduced if you know the seed.
95
99
//!
96
100
//! There are two very different groups interested in developing PRNGs: one
97
- //! interested in simulations and statistics, another in cryptography. From both
98
- //! groups Rand picks one good PRNG algorithm. They are exposed as:
101
+ //! interested in simulations and statistics, another in cryptography (sometimes
102
+ //! PRNGs from the latter are more specifically called a CSPRNG). From both
103
+ //! groups Rand picks one good algorithm. They are exposed as:
99
104
//!
100
105
//! - [`SmallRng`]. Uses little memory, and is very fast.
101
106
//! Note: The currently picked algorithm is not ideal, but in a future version
102
107
//! the values will for all practical purposes be statistically indiscernible
103
108
//! from true randomness.
104
- //! - [`StdRng`]. In addition to the output being indiscernible from true
105
- //! randomness, this PRNG is unpredictable, even if you know the
106
- //! implementation. This requirement makes it slower and/or use more memory
107
- //! however.
109
+ //! - [`StdRng`]. It is a requirement for a CSPRNG that the output is basically
110
+ //! indiscernible from true randomness, as otherwise information could be
111
+ //! leaked. In addition this PRNG is unpredictable, even if you know the
112
+ //! implementation. This is an important property for any situation where
113
+ //! there might be an adversary, like cryptography, but certainly not limited
114
+ //! to that. This requirement makes it slower and/or use more memory however.
108
115
//!
109
116
//! One of the problems with PRNGs is that they need a seed (which is what the
110
117
//! [`SeedableRng`] trait is for). Where do you get such a seed? Two options:
111
118
//!
112
119
//! - Just use a constant. This is good when you need reproducible results, for
113
120
//! example to procedurally generate a game world). See the
114
- //! [`SeedableRng::from_seed`] documentation.
121
+ //! [`SeedableRng::from_seed`] documentation. You may want to combine this
122
+ //! with a named PRNG from the [`prng`] module or from other crates, because
123
+ //! the algorithm of [`StdRng`] and [`SmallRng`] can change per release and/or
124
+ //! platform.
115
125
//! - Get a random value from somewhere at runtime. The
116
126
//! [`FromEntropy::from_entropy`] method helps here. It will usually get the
117
127
//! seed from the operating system's PRNG (using [`EntropyRng`]).
123
133
//!
124
134
//! [`ThreadRng`] abstracts away most of these issues. It uses [`StdRng`], which
125
135
//! is always a safe choice. Seeding happens automatically on first use. The
126
- //! PRNG its state is stored in thread local storage, so no need to plumb the
136
+ //! PRNG's state is stored in thread local storage, so no need to plumb the
127
137
//! state through an API. You can easily get a mutable reference to it with the
128
138
//! [`thread_rng`] function.
129
139
//!
130
140
//! ### Conclusion
131
141
//!
132
142
//! - [`thread_rng`] is what you often want to use.
133
- //! - If you want more control or better performance, use [`StdRng`] or
134
- //! [`SmallRng`].
143
+ //! - If you want more control, flexibility, or better performance, use
144
+ //! [`StdRng`], [` SmallRng`] or choose a specific PRNG algorithm .
135
145
//! - If you do not use [`thread_rng`], use [`FromEntropy::from_entropy`] for
136
146
//! seeding.
137
147
//! - If you need reproducibility, use [`SeedableRng::from_seed`] combined with
138
- //! a named PRNG from the [`prng`] module or other crates (i.e. not
139
- //! [`StdRng`]/[`SmallRng`] which may be adjusted in the future.)
148
+ //! a named PRNG.
140
149
//!
141
150
//! For more information, and notes on cryptographic security, see the
142
151
//! documentation in the [`prng`] module.
143
152
//!
144
153
//! ### Examples
145
154
//!
146
- //! Examples of seeding PRNGs.
155
+ //! Examples of seeding PRNGs:
147
156
//!
148
157
//! ```
149
158
//! # use rand::{Rng, Error};
186
195
//! generate floating point values in the (0, 1) interval.
187
196
//!
188
197
//! For floating point values you may want to use probability distributions.
189
- //! For example time of decay is often modelled with an exponential
198
+ //! For example, time of decay is often modelled with an exponential
190
199
//! distribution [`Exp`], and the log-normal, [`LogNormal`] distribution
191
200
//! provides a good model of many natural phenomona.
192
201
//!
203
212
//!
204
213
//! Sampling from a distribution:
205
214
//!
206
- //! ```rust
215
+ //! ```
207
216
//! use rand::{thread_rng, Rng};
208
217
//! use rand::distributions::Exp;
209
218
//!
214
223
//!
215
224
//! Implementing the [`Standard`] distribution for a user type:
216
225
//!
217
- //! ```rust
226
+ //! ```
218
227
//! # #![allow(dead_code)]
219
228
//! use rand::Rng;
220
229
//! use rand::distributions::{Distribution, Standard};
235
244
//!
236
245
//! Rand contains some more functionality not discussed above.
237
246
//!
247
+ //! - [`Rng::fill`] and [`Rng::try_fill`] as fast alternatives to fill a slice
248
+ //! of integers.
238
249
//! - [`Rng::shuffle`] can be used to shuffle slices.
239
250
//! - [`Rng::choose`] to pick one element at random from a slice, and the
240
- //! functions in the [`seq`] the pick multiple.
251
+ //! functions in the [`seq`] module to the pick multiple.
241
252
//! - [`Rng::sample_iter`] to get an iterator for the choosen distribution.
242
253
//! - [`Rng::gen_bool`] to generate events with weighted probability.
243
254
//! - [`distributions::WeightedChoice`] can be used to pick elements at random
283
294
//!
284
295
//! ### Example
285
296
//!
286
- //! ```rust
297
+ //! ```
287
298
//! # use rand::thread_rng;
288
299
//! use rand::Rng;
289
300
//!
308
319
//! [`Open01`]: distributions/struct.Open01.html
309
320
//! [`OsRng`]: os/struct.OsRng.html
310
321
//! [`Rng::choose`]: trait.Rng.html#method.choose
322
+ //! [`Rng::fill`]: trait.Rng.html#method.fill
323
+ //! [`Rng::try_fill`]: trait.Rng.html#method.try_fill
311
324
//! [`Rng::gen_bool`]: trait.Rng.html#method.gen_bool
312
325
//! [`Rng::gen_range`]: trait.Rng.html#method.gen_range
313
326
//! [`Rng::gen()`]: trait.Rng.html#method.gen
@@ -442,7 +455,7 @@ pub trait Rng: RngCore {
442
455
///
443
456
/// # Example
444
457
///
445
- /// ```rust
458
+ /// ```
446
459
/// use rand::{thread_rng, Rng};
447
460
///
448
461
/// let mut rng = thread_rng();
@@ -471,7 +484,7 @@ pub trait Rng: RngCore {
471
484
///
472
485
/// # Example
473
486
///
474
- /// ```rust
487
+ /// ```
475
488
/// use rand::{thread_rng, Rng};
476
489
///
477
490
/// let mut rng = thread_rng();
@@ -488,7 +501,7 @@ pub trait Rng: RngCore {
488
501
///
489
502
/// ### Example
490
503
///
491
- /// ```rust
504
+ /// ```
492
505
/// use rand::{thread_rng, Rng};
493
506
/// use rand::distributions::Uniform;
494
507
///
@@ -503,7 +516,7 @@ pub trait Rng: RngCore {
503
516
///
504
517
/// # Example
505
518
///
506
- /// ```rust
519
+ /// ```
507
520
/// use rand::{thread_rng, Rng};
508
521
/// use rand::distributions::{Alphanumeric, Uniform, Standard};
509
522
///
@@ -545,7 +558,7 @@ pub trait Rng: RngCore {
545
558
///
546
559
/// # Example
547
560
///
548
- /// ```rust
561
+ /// ```
549
562
/// use rand::{thread_rng, Rng};
550
563
///
551
564
/// let mut arr = [0i8; 20];
@@ -574,7 +587,7 @@ pub trait Rng: RngCore {
574
587
///
575
588
/// # Example
576
589
///
577
- /// ```rust
590
+ /// ```
578
591
/// # use rand::Error;
579
592
/// use rand::{thread_rng, Rng};
580
593
///
@@ -601,7 +614,7 @@ pub trait Rng: RngCore {
601
614
///
602
615
/// # Example
603
616
///
604
- /// ```rust
617
+ /// ```
605
618
/// use rand::{thread_rng, Rng};
606
619
///
607
620
/// let mut rng = thread_rng();
@@ -666,7 +679,7 @@ pub trait Rng: RngCore {
666
679
///
667
680
/// # Example
668
681
///
669
- /// ```rust
682
+ /// ```
670
683
/// use rand::{thread_rng, Rng};
671
684
///
672
685
/// let mut rng = thread_rng();
@@ -711,7 +724,7 @@ pub trait Rng: RngCore {
711
724
///
712
725
/// # Example
713
726
///
714
- /// ```rust
727
+ /// ```
715
728
/// # #![allow(deprecated)]
716
729
/// use rand::{thread_rng, Rng};
717
730
///
@@ -734,7 +747,7 @@ pub trait Rng: RngCore {
734
747
///
735
748
/// # Example
736
749
///
737
- /// ```rust
750
+ /// ```
738
751
/// # #![allow(deprecated)]
739
752
/// use rand::{thread_rng, Rng};
740
753
///
@@ -931,7 +944,7 @@ pub trait FromEntropy: SeedableRng {
931
944
/// If all entropy sources fail this will panic. If you need to handle
932
945
/// errors, use the following code, equivalent aside from error handling:
933
946
///
934
- /// ```rust
947
+ /// ```
935
948
/// # use rand::Error;
936
949
/// use rand::{Rng, StdRng, EntropyRng, SeedableRng};
937
950
///
@@ -1118,7 +1131,7 @@ pub fn weak_rng() -> XorShiftRng {
1118
1131
///
1119
1132
/// # Example
1120
1133
///
1121
- /// ```rust
1134
+ /// ```
1122
1135
/// # #![allow(deprecated)]
1123
1136
/// use rand::{thread_rng, sample};
1124
1137
///
0 commit comments