6
6
[ ![ Documentation] ( https://docs.rs/rand/badge.svg )] ( https://docs.rs/rand )
7
7
[ ![ Minimum rustc version] ( https://img.shields.io/badge/rustc-1.22+-yellow.svg )] ( https://github.com/rust-lang-nursery/rand#rust-version-requirements )
8
8
9
- A Rust library for random number generators and other randomness functionality .
9
+ A Rust library for random number generation .
10
10
11
- See also:
11
+ Rand provides utilities to generate random numbers, to convert them to useful
12
+ types and distributions, and some randomness-related algorithms.
12
13
13
- * [ rand_core] ( https://crates.io/crates/rand_core )
14
+ The core random number generation traits of Rand live in the [ rand_core] (
15
+ https://crates.io/crates/rand_core ) crate; this crate is most useful when
16
+ implementing RNGs.
14
17
15
- Documentation :
18
+ API reference :
16
19
[ master branch] ( https://rust-lang-nursery.github.io/rand/rand/index.html ) ,
17
- [ by release] ( https://docs.rs/rand )
20
+ [ by release] ( https://docs.rs/rand/0.5 ) .
18
21
19
22
## Usage
20
23
@@ -30,14 +33,57 @@ and this to your crate root:
30
33
``` rust
31
34
extern crate rand;
32
35
33
- // example usage:
34
- use rand :: {Rng , thread_rng};
35
- let x : f64 = thread_rng (). gen ();
36
+ use rand :: prelude :: * ;
37
+
38
+ // basic usage with random():
39
+ let x : u8 = random ();
40
+ println! (" {}" , x );
41
+
42
+ let y = random :: <f64 >();
43
+ println! (" {}" , y );
44
+
45
+ if random () { // generates a boolean
46
+ println! (" Heads!" );
47
+ }
48
+
49
+ // normal usage needs both an RNG and a function to generate the appropriate
50
+ // type, range, distribution, etc.
51
+ let mut rng = thread_rng ();
52
+ if rng . gen () { // random bool
53
+ let x : f64 = rng . gen (); // random number in range (0, 1)
54
+ println! (" x is: {}" , x );
55
+ let char = rng . gen :: <char >(); // Sometimes you need type annotation
56
+ println! (" char is: {}" , char );
57
+ println! (" Number from 0 to 9: {}" , rng . gen_range (0 , 10 ));
58
+ }
36
59
```
37
60
61
+ ## Functionality
62
+
63
+ The Rand crate provides:
64
+
65
+ - A convenient to use default RNG, ` thread_rng ` : an automatically seeded,
66
+ crypto-grade generator stored in thread-local memory.
67
+ - Pseudo-random number generators: ` StdRng ` , ` SmallRng ` , ` prng ` module.
68
+ - Functionality for seeding PRNGs: the ` FromEntropy ` trait, and as sources of
69
+ external randomness ` EntropyRng ` , ` OsRng ` and ` JitterRng ` .
70
+ - Most content from [ ` rand_core ` ] ( https://crates.io/crates/rand_core )
71
+ (re-exported): base random number generator traits and error-reporting types.
72
+ - 'Distributions' producing many different types of random values:
73
+ - A ` Standard ` distribution for integers, floats, and derived types including
74
+ tuples, arrays and ` Option `
75
+ - Unbiased sampling from specified ` Uniform ` ranges.
76
+ - Sampling from exponential/normal/gamma distributions.
77
+ - Sampling from binomial/poisson distributions.
78
+ - ` gen_bool ` aka Bernoulli distribution.
79
+ - ` seq ` -uence related functionality:
80
+ - Sampling a subset of elements.
81
+ - Randomly shuffling a list.
82
+
83
+
38
84
## Versions
39
85
40
- Version 0.5 is available as a pre-release and contains many breaking changes.
86
+ Version 0.5 is the latest version and contains many breaking changes.
41
87
See [ the Upgrade Guide] ( UPDATING.md ) for guidance on updating from previous
42
88
versions.
43
89
@@ -46,21 +92,9 @@ changes since the 0.3 series.
46
92
47
93
For more details, see the [ changelog] ( CHANGELOG.md ) .
48
94
49
- ### Compatibility shims
50
-
51
- ** As of now there is no compatibility shim between Rand 0.4 and 0.5.**
52
- It is also not entirely obvious how to make one due to the large differences
53
- between the two versions, although it would be possible to implement the new
54
- ` RngCore ` for any implementation of the old ` Rng ` (or vice-versa; unfortunately
55
- not both as that would result in circular implementation). If we implement a
56
- compatibility shim it will be optional (opt-in via a feature).
57
-
58
- There is a compatibility shim from 0.3 to 0.4 forcibly upgrading all Rand 0.3
59
- users; this is largely due to the small differences between the two versions.
60
-
61
95
### Rust version requirements
62
96
63
- The 0.5 release of Rand will require ** Rustc version 1.22 or greater** .
97
+ The 0.5 release of Rand requires ** Rustc version 1.22 or greater** .
64
98
Rand 0.4 and 0.3 (since approx. June 2017) require Rustc version 1.15 or
65
99
greater. Subsets of the Rand code may work with older Rust versions, but this
66
100
is not supported.
@@ -69,84 +103,38 @@ Travis CI always has a build with a pinned version of Rustc matching the oldest
69
103
supported Rust release. The current policy is that this can be updated in any
70
104
Rand release if required, but the change must be noted in the changelog.
71
105
72
- ## Functionality
73
-
74
- The ` rand_core ` crate provides:
75
-
76
- - base random number generator traits
77
- - error-reporting types
78
- - functionality to aid implementation of RNGs
79
-
80
- The ` rand ` crate provides:
81
-
82
- - most content from ` rand_core ` (re-exported)
83
- - fresh entropy: ` EntropyRng ` , ` OsRng ` , ` JitterRng `
84
- - pseudo-random number generators: ` StdRng ` , ` SmallRng ` , ` prng ` module
85
- - convenient, auto-seeded crypto-grade thread-local generator: ` thread_rng `
86
- - ` distributions ` producing many different types of random values:
87
- - a ` Standard ` distribution for integers, floats,and derived types
88
- including tuples, arrays and ` Option `
89
- - unbiased sampling from specified ` Range ` s
90
- - sampling from exponential/normal/gamma distributions
91
- - sampling from binomial/poisson distributions
92
- - ` gen_bool ` aka Bernoulli distribution
93
- - ` seq ` -uence related functionality:
94
- - sampling a subset of elements
95
- - randomly shuffling a list
96
106
97
107
## Crate Features
98
108
99
- By default, Rand is built with all stable features available . The following
109
+ Rand is built with only the ` std ` feature anabled by default . The following
100
110
optional features are available:
101
111
102
- - ` alloc ` can be used instead of ` std ` to provide ` Vec ` and ` Box `
103
- - ` i128_support ` enables support for generating ` u128 ` and ` i128 ` values
104
- - ` log ` enables some logging via the ` log ` crate
105
- - ` nightly ` enables all unstable features (` i128_support ` )
106
- - ` serde1 ` enables serialization for some types, via Serde version 1
107
- - ` stdweb ` enables support for ` OsRng ` on WASM via stdweb.
108
- - ` std ` enabled by default; by setting "default-features = false" ` no_std `
109
- mode is activated; this removes features depending on ` std ` functionality:
110
- - ` OsRng ` is entirely unavailable
111
- - ` JitterRng ` code is still present, but a nanosecond timer must be
112
- provided via ` JitterRng::new_with_timer `
113
- - Since no external entropy is available, it is not possible to create
114
- generators with fresh seeds (user must provide entropy)
115
- - ` thread_rng ` , ` weak_rng ` and ` random ` are all disabled
116
- - exponential, normal and gamma type distributions are unavailable
117
- since ` exp ` and ` log ` functions are not provided in ` core `
118
- - any code requiring ` Vec ` or ` Box `
119
-
120
- ## Testing
121
-
122
- Unfortunately, ` cargo test ` does not test everything. The following tests are
123
- recommended:
124
-
125
- ```
126
- # Basic tests for Rand and sub-crates
127
- cargo test --all
128
-
129
- # Test no_std support
130
- cargo test --tests --no-default-features
131
- # Test no_std+alloc support
132
- cargo test --tests --no-default-features --features alloc
133
-
134
- # Test log and serde support
135
- cargo test --features serde1,log
136
-
137
- # Test 128-bit support (requires nightly)
138
- cargo test --all --features nightly
139
-
140
- # Benchmarks (requires nightly)
141
- cargo bench
142
- # or just to test the benchmark code:
143
- cargo test --benches
144
- ```
112
+ - ` alloc ` can be used instead of ` std ` to provide ` Vec ` and ` Box ` .
113
+ - ` i128_support ` enables support for generating ` u128 ` and ` i128 ` values.
114
+ - ` log ` enables some logging via the ` log ` crate.
115
+ - ` nightly ` enables all unstable features (` i128_support ` ).
116
+ - ` serde1 ` enables serialization for some types, via Serde version 1.
117
+ - ` stdweb ` enables support for ` OsRng ` on WASM via stdweb.
118
+
119
+ ` no_std ` mode is activated by setting ` default-features = false ` ; this removes
120
+ functionality depending on ` std ` :
121
+
122
+ - ` thread_rng() ` , and ` random() ` are not available, as they require thread-local
123
+ storage and an entropy source.
124
+ - ` OsRng ` and ` EntropyRng ` are unavailable.
125
+ - ` JitterRng ` code is still present, but a nanosecond timer must be provided via
126
+ ` JitterRng::new_with_timer `
127
+ - Since no external entropy is available, it is not possible to create
128
+ generators with fresh seeds using the ` FromEntropy ` trait (user must provide
129
+ a seed).
130
+ - Exponential, normal and gamma type distributions are unavailable since ` exp `
131
+ and ` log ` functions are not provided in ` core ` .
132
+ - The ` seq ` -uence module is unavailable, as it requires ` Vec ` .
145
133
146
134
147
135
# License
148
136
149
- Rand is distributed under the terms of both the MIT
150
- license and the Apache License (Version 2.0).
137
+ Rand is distributed under the terms of both the MIT license and the
138
+ Apache License (Version 2.0).
151
139
152
140
See [ LICENSE-APACHE] ( LICENSE-APACHE ) and [ LICENSE-MIT] ( LICENSE-MIT ) for details.
0 commit comments