• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

benches/H03-May-2022-306260

src/H03-May-2022-6,3733,705

utils/H03-May-2022-12859

.cargo-checksum.jsonH A D03-May-202289 11

.gitignoreH A D05-Dec-201718 32

.travis.ymlH A D06-Jan-2018811 3428

CHANGELOG.mdH A D06-Jan-20186.7 KiB266187

Cargo.tomlH A D01-Jan-19701.3 KiB4037

Cargo.toml.orig-cargoH A D06-Jan-20181,020 3628

LICENSE-APACHEH A D05-Dec-201710.6 KiB202169

LICENSE-MITH A D05-Dec-20171 KiB2622

README.mdH A D06-Jan-20184.1 KiB143101

appveyor.ymlH A D06-Jan-20181.5 KiB3934

README.md

1rand
2====
3
4A Rust library for random number generators and other randomness functionality.
5
6[![Build Status](https://travis-ci.org/rust-lang-nursery/rand.svg?branch=master)](https://travis-ci.org/rust-lang-nursery/rand)
7[![Build status](https://ci.appveyor.com/api/projects/status/rm5c9o33k3jhchbw?svg=true)](https://ci.appveyor.com/project/alexcrichton/rand)
8
9[Documentation](https://docs.rs/rand)
10
11## Usage
12
13Add this to your `Cargo.toml`:
14
15```toml
16[dependencies]
17rand = "0.4"
18```
19
20and this to your crate root:
21
22```rust
23extern crate rand;
24```
25
26### Versions
27
28The `rand` crate has been at version `0.3` since March 2015. If you wish to
29avoid all breaking changes you may wish to stick with this version.
30
31Version `0.4`was released in December 2017. It contains almost no breaking
32changes since the `0.3` series, but nevertheless contains some significant
33new code, including a new "external" entropy source (`JitterRng`) and `no_std`
34support.
35
36Version `0.5` is in development and contains significant performance
37improvements for the ISAAC random number generators.
38
39## Examples
40
41There is built-in support for a random number generator (RNG) associated with each thread stored in thread-local storage. This RNG can be accessed via thread_rng, or used implicitly via random. This RNG is normally randomly seeded from an operating-system source of randomness, e.g. /dev/urandom on Unix systems, and will automatically reseed itself from this source after generating 32 KiB of random data.
42
43```rust
44let tuple = rand::random::<(f64, char)>();
45println!("{:?}", tuple)
46```
47
48```rust
49use rand::Rng;
50
51let mut rng = rand::thread_rng();
52if rng.gen() { // random bool
53    println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
54}
55```
56
57It is also possible to use other RNG types, which have a similar interface. The following uses the "ChaCha" algorithm instead of the default.
58
59```rust
60use rand::{Rng, ChaChaRng};
61
62let mut rng = rand::ChaChaRng::new_unseeded();
63println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
64```
65
66## Features
67
68By default, `rand` is built with all stable features available. The following
69optional features are available:
70
71-   `i128_support` enables support for generating `u128` and `i128` values
72-   `nightly` enables all unstable features (`i128_support`)
73-   `std` enabled by default; by setting "default-features = false" `no_std`
74    mode is activated; this removes features depending on `std` functionality:
75
76        -   `OsRng` is entirely unavailable
77        -   `JitterRng` code is still present, but a nanosecond timer must be
78            provided via `JitterRng::new_with_timer`
79        -   Since no external entropy is available, it is not possible to create
80            generators with fresh seeds (user must provide entropy)
81        -   `thread_rng`, `weak_rng` and `random` are all disabled
82        -   exponential, normal and gamma type distributions are unavailable
83            since `exp` and `log` functions are not provided in `core`
84        -   any code requiring `Vec` or `Box`
85-   `alloc` can be used instead of `std` to provide `Vec` and `Box`
86
87## Testing
88
89Unfortunately, `cargo test` does not test everything. The following tests are
90recommended:
91
92```
93# Basic tests for rand and sub-crates
94cargo test --all
95
96# Test no_std support (build only since nearly all tests require std)
97cargo build --all --no-default-features
98
99# Test 128-bit support (requires nightly)
100cargo test --all --features nightly
101
102# Benchmarks (requires nightly)
103cargo bench
104# or just to test the benchmark code:
105cargo test --benches
106```
107
108# `derive(Rand)`
109
110You can derive the `Rand` trait for your custom type via the `#[derive(Rand)]`
111directive. To use this first add this to your Cargo.toml:
112
113```toml
114rand = "0.4"
115rand_derive = "0.3"
116```
117
118Next in your crate:
119
120```rust
121extern crate rand;
122#[macro_use]
123extern crate rand_derive;
124
125#[derive(Rand, Debug)]
126struct MyStruct {
127    a: i32,
128    b: u32,
129}
130
131fn main() {
132    println!("{:?}", rand::random::<MyStruct>());
133}
134```
135
136
137# License
138
139`rand` is primarily distributed under the terms of both the MIT
140license and the Apache License (Version 2.0).
141
142See LICENSE-APACHE, and LICENSE-MIT for details.
143