| 1 |
# Rand |
| 2 |
|
| 3 |
[](https://github.com/rust-random/rand/actions) |
| 4 |
[](https://crates.io/crates/rand) |
| 5 |
[](https://rust-random.github.io/book/) |
| 6 |
[](https://rust-random.github.io/rand/rand) |
| 7 |
[](https://docs.rs/rand) |
| 8 |
[](https://github.com/rust-random/rand#rust-version-requirements) |
| 9 |
|
| 10 |
A Rust library for random number generation, featuring: |
| 11 |
|
| 12 |
- Easy random value generation and usage via the [`Rng`](https://docs.rs/rand/*/rand/trait.Rng.html), |
| 13 |
[`SliceRandom`](https://docs.rs/rand/*/rand/seq/trait.SliceRandom.html) and |
| 14 |
[`IteratorRandom`](https://docs.rs/rand/*/rand/seq/trait.IteratorRandom.html) traits |
| 15 |
- Secure seeding via the [`getrandom` crate](https://crates.io/crates/getrandom) |
| 16 |
and fast, convenient generation via [`thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html) |
| 17 |
- A modular design built over [`rand_core`](https://crates.io/crates/rand_core) |
| 18 |
([see the book](https://rust-random.github.io/book/crates.html)) |
| 19 |
- Fast implementations of the best-in-class [cryptographic](https://rust-random.github.io/book/guide-rngs.html#cryptographically-secure-pseudo-random-number-generators-csprngs) and |
| 20 |
[non-cryptographic](https://rust-random.github.io/book/guide-rngs.html#basic-pseudo-random-number-generators-prngs) generators |
| 21 |
- A flexible [`distributions`](https://docs.rs/rand/*/rand/distributions/index.html) module |
| 22 |
- Samplers for a large number of random number distributions via our own |
| 23 |
[`rand_distr`](https://docs.rs/rand_distr) and via |
| 24 |
the [`statrs`](https://docs.rs/statrs/0.13.0/statrs/) |
| 25 |
- [Portably reproducible output](https://rust-random.github.io/book/portability.html) |
| 26 |
- `#[no_std]` compatibility (partial) |
| 27 |
- *Many* performance optimisations |
| 28 |
|
| 29 |
It's also worth pointing out what `rand` *is not*: |
| 30 |
|
| 31 |
- Small. Most low-level crates are small, but the higher-level `rand` and |
| 32 |
`rand_distr` each contain a lot of functionality. |
| 33 |
- Simple (implementation). We have a strong focus on correctness, speed and flexibility, but |
| 34 |
not simplicity. If you prefer a small-and-simple library, there are |
| 35 |
alternatives including [fastrand](https://crates.io/crates/fastrand) |
| 36 |
and [oorandom](https://crates.io/crates/oorandom). |
| 37 |
- Slow. We take performance seriously, with considerations also for set-up |
| 38 |
time of new distributions, commonly-used parameters, and parameters of the |
| 39 |
current sampler. |
| 40 |
|
| 41 |
Documentation: |
| 42 |
|
| 43 |
- [The Rust Rand Book](https://rust-random.github.io/book) |
| 44 |
- [API reference (master branch)](https://rust-random.github.io/rand) |
| 45 |
- [API reference (docs.rs)](https://docs.rs/rand) |
| 46 |
|
| 47 |
|
| 48 |
## Usage |
| 49 |
|
| 50 |
Add this to your `Cargo.toml`: |
| 51 |
|
| 52 |
```toml |
| 53 |
[dependencies] |
| 54 |
rand = "0.8.4" |
| 55 |
``` |
| 56 |
|
| 57 |
To get started using Rand, see [The Book](https://rust-random.github.io/book). |
| 58 |
|
| 59 |
|
| 60 |
## Versions |
| 61 |
|
| 62 |
Rand is *mature* (suitable for general usage, with infrequent breaking releases |
| 63 |
which minimise breakage) but not yet at 1.0. We maintain compatibility with |
| 64 |
pinned versions of the Rust compiler (see below). |
| 65 |
|
| 66 |
Current Rand versions are: |
| 67 |
|
| 68 |
- Version 0.7 was released in June 2019, moving most non-uniform distributions |
| 69 |
to an external crate, moving `from_entropy` to `SeedableRng`, and many small |
| 70 |
changes and fixes. |
| 71 |
- Version 0.8 was released in December 2020 with many small changes. |
| 72 |
|
| 73 |
A detailed [changelog](CHANGELOG.md) is available for releases. |
| 74 |
|
| 75 |
When upgrading to the next minor series (especially 0.4 → 0.5), we recommend |
| 76 |
reading the [Upgrade Guide](https://rust-random.github.io/book/update.html). |
| 77 |
|
| 78 |
Rand has not yet reached 1.0 implying some breaking changes may arrive in the |
| 79 |
future ([SemVer](https://semver.org/) allows each 0.x.0 release to include |
| 80 |
breaking changes), but is considered *mature*: breaking changes are minimised |
| 81 |
and breaking releases are infrequent. |
| 82 |
|
| 83 |
Rand libs have inter-dependencies and make use of the |
| 84 |
[semver trick](https://github.com/dtolnay/semver-trick/) in order to make traits |
| 85 |
compatible across crate versions. (This is especially important for `RngCore` |
| 86 |
and `SeedableRng`.) A few crate releases are thus compatibility shims, |
| 87 |
depending on the *next* lib version (e.g. `rand_core` versions `0.2.2` and |
| 88 |
`0.3.1`). This means, for example, that `rand_core_0_4_0::SeedableRng` and |
| 89 |
`rand_core_0_3_0::SeedableRng` are distinct, incompatible traits, which can |
| 90 |
cause build errors. Usually, running `cargo update` is enough to fix any issues. |
| 91 |
|
| 92 |
### Yanked versions |
| 93 |
|
| 94 |
Some versions of Rand crates have been yanked ("unreleased"). Where this occurs, |
| 95 |
the crate's CHANGELOG *should* be updated with a rationale, and a search on the |
| 96 |
issue tracker with the keyword `yank` *should* uncover the motivation. |
| 97 |
|
| 98 |
### Rust version requirements |
| 99 |
|
| 100 |
Since version 0.8, Rand requires **Rustc version 1.36 or greater**. |
| 101 |
Rand 0.7 requires Rustc 1.32 or greater while versions 0.5 require Rustc 1.22 or |
| 102 |
greater, and 0.4 and 0.3 (since approx. June 2017) require Rustc version 1.15 or |
| 103 |
greater. Subsets of the Rand code may work with older Rust versions, but this is |
| 104 |
not supported. |
| 105 |
|
| 106 |
Continuous Integration (CI) will always test the minimum supported Rustc version |
| 107 |
(the MSRV). The current policy is that this can be updated in any |
| 108 |
Rand release if required, but the change must be noted in the changelog. |
| 109 |
|
| 110 |
## Crate Features |
| 111 |
|
| 112 |
Rand is built with these features enabled by default: |
| 113 |
|
| 114 |
- `std` enables functionality dependent on the `std` lib |
| 115 |
- `alloc` (implied by `std`) enables functionality requiring an allocator |
| 116 |
- `getrandom` (implied by `std`) is an optional dependency providing the code |
| 117 |
behind `rngs::OsRng` |
| 118 |
- `std_rng` enables inclusion of `StdRng`, `thread_rng` and `random` |
| 119 |
(the latter two *also* require that `std` be enabled) |
| 120 |
|
| 121 |
Optionally, the following dependencies can be enabled: |
| 122 |
|
| 123 |
- `log` enables logging via the `log` crate |
| 124 |
|
| 125 |
Additionally, these features configure Rand: |
| 126 |
|
| 127 |
- `small_rng` enables inclusion of the `SmallRng` PRNG |
| 128 |
- `nightly` enables some optimizations requiring nightly Rust |
| 129 |
- `simd_support` (experimental) enables sampling of SIMD values |
| 130 |
(uniformly random SIMD integers and floats), requiring nightly Rust |
| 131 |
- `min_const_gen` enables generating random arrays of |
| 132 |
any size using min-const-generics, requiring Rust ≥ 1.51. |
| 133 |
|
| 134 |
Note that nightly features are not stable and therefore not all library and |
| 135 |
compiler versions will be compatible. This is especially true of Rand's |
| 136 |
experimental `simd_support` feature. |
| 137 |
|
| 138 |
Rand supports limited functionality in `no_std` mode (enabled via |
| 139 |
`default-features = false`). In this case, `OsRng` and `from_entropy` are |
| 140 |
unavailable (unless `getrandom` is enabled), large parts of `seq` are |
| 141 |
unavailable (unless `alloc` is enabled), and `thread_rng` and `random` are |
| 142 |
unavailable. |
| 143 |
|
| 144 |
### WASM support |
| 145 |
|
| 146 |
The WASM target `wasm32-unknown-unknown` is not *automatically* supported by |
| 147 |
`rand` or `getrandom`. To solve this, either use a different target such as |
| 148 |
`wasm32-wasi` or add a direct dependency on `getrandom` with the `js` feature |
| 149 |
(if the target supports JavaScript). See |
| 150 |
[getrandom#WebAssembly support](https://docs.rs/getrandom/latest/getrandom/#webassembly-support). |
| 151 |
|
| 152 |
# License |
| 153 |
|
| 154 |
Rand is distributed under the terms of both the MIT license and the |
| 155 |
Apache License (Version 2.0). |
| 156 |
|
| 157 |
See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT), and |
| 158 |
[COPYRIGHT](COPYRIGHT) for details. |