1# Rand
2
3[![Build Status](https://travis-ci.org/rust-lang-nursery/rand.svg?branch=master)](https://travis-ci.org/rust-lang-nursery/rand)
4[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-lang-nursery/rand?svg=true)](https://ci.appveyor.com/project/alexcrichton/rand)
5[![Latest version](https://img.shields.io/crates/v/rand.svg)](https://crates.io/crates/rand)
6[![Documentation](https://docs.rs/rand/badge.svg)](https://docs.rs/rand)
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
9A Rust library for random number generation.
10
11Rand provides utilities to generate random numbers, to convert them to useful
12types and distributions, and some randomness-related algorithms.
13
14The core random number generation traits of Rand live in the [rand_core](
15https://crates.io/crates/rand_core) crate; this crate is most useful when
16implementing RNGs.
17
18API reference:
19[master branch](https://rust-lang-nursery.github.io/rand/rand/index.html),
20[by release](https://docs.rs/rand/0.5).
21
22## Usage
23
24Add this to your `Cargo.toml`:
25
26```toml
27[dependencies]
28rand = "0.5"
29```
30
31and this to your crate root:
32
33```rust
34extern crate rand;
35
36use rand::prelude::*;
37
38fn main() {
39  // basic usage with random():
40  let x: u8 = random();
41  println!("{}", x);
42
43  let y = random::<f64>();
44  println!("{}", y);
45
46  if random() { // generates a boolean
47      println!("Heads!");
48  }
49
50  // normal usage needs both an RNG and a function to generate the appropriate
51  // type, range, distribution, etc.
52  let mut rng = thread_rng();
53  if rng.gen() { // random bool
54      let x: f64 = rng.gen(); // random number in range [0, 1)
55      println!("x is: {}", x);
56      let ch = rng.gen::<char>(); // Sometimes you need type annotation
57      println!("char is: {}", ch);
58      println!("Number from 0 to 9: {}", rng.gen_range(0, 10));
59  }
60}
61```
62
63## Functionality
64
65The Rand crate provides:
66
67- A convenient to use default RNG, `thread_rng`: an automatically seeded,
68  crypto-grade generator stored in thread-local memory.
69- Pseudo-random number generators: `StdRng`, `SmallRng`, `prng` module.
70- Functionality for seeding PRNGs: the `FromEntropy` trait, and as sources of
71  external randomness `EntropyRng`, `OsRng` and `JitterRng`.
72- Most content from [`rand_core`](https://crates.io/crates/rand_core)
73  (re-exported): base random number generator traits and error-reporting types.
74- 'Distributions' producing many different types of random values:
75  - A `Standard` distribution for integers, floats, and derived types including
76    tuples, arrays and `Option`
77  - Unbiased sampling from specified `Uniform` ranges.
78  - Sampling from exponential/normal/gamma distributions.
79  - Sampling from binomial/poisson distributions.
80  - `gen_bool` aka Bernoulli distribution.
81- `seq`-uence related functionality:
82  - Sampling a subset of elements.
83  - Randomly shuffling a list.
84
85
86## Versions
87
88Version 0.5 is the latest version and contains many breaking changes.
89See [the Upgrade Guide](UPDATING.md) for guidance on updating from previous
90versions.
91
92Version 0.4 was released in December 2017. It contains almost no breaking
93changes since the 0.3 series.
94
95For more details, see the [changelog](CHANGELOG.md).
96
97### Rust version requirements
98
99The 0.5 release of Rand requires **Rustc version 1.22 or greater**.
100Rand 0.4 and 0.3 (since approx. June 2017) require Rustc version 1.15 or
101greater. Subsets of the Rand code may work with older Rust versions, but this
102is not supported.
103
104Travis CI always has a build with a pinned version of Rustc matching the oldest
105supported Rust release. The current policy is that this can be updated in any
106Rand release if required, but the change must be noted in the changelog.
107
108
109## Crate Features
110
111Rand is built with only the `std` feature enabled by default. The following
112optional features are available:
113
114- `alloc` can be used instead of `std` to provide `Vec` and `Box`.
115- `i128_support` enables support for generating `u128` and `i128` values.
116- `log` enables some logging via the `log` crate.
117- `nightly` enables all unstable features (`i128_support`).
118- `serde1` enables serialization for some types, via Serde version 1.
119- `stdweb` enables support for `OsRng` on `wasm-unknown-unknown` via `stdweb`
120  combined with `cargo-web`.
121
122`no_std` mode is activated by setting `default-features = false`; this removes
123functionality depending on `std`:
124
125- `thread_rng()`, and `random()` are not available, as they require thread-local
126  storage and an entropy source.
127- `OsRng` and `EntropyRng` are unavailable.
128- `JitterRng` code is still present, but a nanosecond timer must be provided via
129  `JitterRng::new_with_timer`
130- Since no external entropy is available, it is not possible to create
131  generators with fresh seeds using the `FromEntropy` trait (user must provide
132  a seed).
133- Exponential, normal and gamma type distributions are unavailable since `exp`
134  and `log` functions are not provided in `core`.
135- The `seq`-uence module is unavailable, as it requires `Vec`.
136
137
138# License
139
140Rand is distributed under the terms of both the MIT license and the
141Apache License (Version 2.0).
142
143See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) for details.
144