1# Rand
2
3[![Test Status](https://github.com/rust-random/rand/workflows/Tests/badge.svg?event=push)](https://github.com/rust-random/rand/actions)
4[![Crate](https://img.shields.io/crates/v/rand.svg)](https://crates.io/crates/rand)
5[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
6[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand)
7[![API](https://docs.rs/rand/badge.svg)](https://docs.rs/rand)
8[![Minimum rustc version](https://img.shields.io/badge/rustc-1.36+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
9
10A 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
29It'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
41Documentation:
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
50Add this to your `Cargo.toml`:
51
52```toml
53[dependencies]
54rand = "0.8.0"
55```
56
57To get started using Rand, see [The Book](https://rust-random.github.io/book).
58
59
60## Versions
61
62Rand is *mature* (suitable for general usage, with infrequent breaking releases
63which minimise breakage) but not yet at 1.0. We maintain compatibility with
64pinned versions of the Rust compiler (see below).
65
66Current 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
73A detailed [changelog](CHANGELOG.md) is available for releases.
74
75When upgrading to the next minor series (especially 0.4 → 0.5), we recommend
76reading the [Upgrade Guide](https://rust-random.github.io/book/update.html).
77
78Rand has not yet reached 1.0 implying some breaking changes may arrive in the
79future ([SemVer](https://semver.org/) allows each 0.x.0 release to include
80breaking changes), but is considered *mature*: breaking changes are minimised
81and breaking releases are infrequent.
82
83Rand libs have inter-dependencies and make use of the
84[semver trick](https://github.com/dtolnay/semver-trick/) in order to make traits
85compatible across crate versions. (This is especially important for `RngCore`
86and `SeedableRng`.) A few crate releases are thus compatibility shims,
87depending 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
90cause build errors. Usually, running `cargo update` is enough to fix any issues.
91
92### Yanked versions
93
94Some versions of Rand crates have been yanked ("unreleased"). Where this occurs,
95the crate's CHANGELOG *should* be updated with a rationale, and a search on the
96issue tracker with the keyword `yank` *should* uncover the motivation.
97
98### Rust version requirements
99
100Since version 0.8, Rand requires **Rustc version 1.36 or greater**.
101Rand 0.7 requires Rustc 1.32 or greater while versions 0.5 require Rustc 1.22 or
102greater, and 0.4 and 0.3 (since approx. June 2017) require Rustc version 1.15 or
103greater. Subsets of the Rand code may work with older Rust versions, but this is
104not supported.
105
106Continuous Integration (CI) will always test the minimum supported Rustc version
107(the MSRV). The current policy is that this can be updated in any
108Rand release if required, but the change must be noted in the changelog.
109
110## Crate Features
111
112Rand 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
121Optionally, the following dependencies can be enabled:
122
123-   `log` enables logging via the `log` crate` crate
124
125Additionally, 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
132Note that nightly features are not stable and therefore not all library and
133compiler versions will be compatible. This is especially true of Rand's
134experimental `simd_support` feature.
135
136Rand supports limited functionality in `no_std` mode (enabled via
137`default-features = false`). In this case, `OsRng` and `from_entropy` are
138unavailable (unless `getrandom` is enabled), large parts of `seq` are
139unavailable (unless `alloc` is enabled), and `thread_rng` and `random` are
140unavailable.
141
142# License
143
144Rand is distributed under the terms of both the MIT license and the
145Apache License (Version 2.0).
146
147See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT), and
148[COPYRIGHT](COPYRIGHT) for details.
149