1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! This crate provides a lightweight alternative to rand,
//! using the "xoshiro256++" (<https://prng.di.unimi.it>)
//! and "ChaCha12" algorithms (<https://cr.yp.to/chacha.html>),
//! which are also the ones used by [`rand`](https://crates.io/crates/rand) for its SmallRng
//! and StdRng, respectively.
//!
//! The crate is intended to be easy to audit.
//! Its only dependency is [`getrandom`](https://crates.io/crates/getrandom),
//! and that is only used on non-Linux/Unix platforms.
//! It can also be built as no-std, in which case [`getrandom`](https://crates.io/crates/getrandom)
//! is not used at all (but you´ll then have to provide the seed yourself).
//!
//! Basic usage:
//!
//! ```
//! #[cfg(feature = "std")]
//! {
//! use smallrand::StdRng;
//! let mut rng = StdRng::new();
//! let coin_flip : bool = rng.random();
//! let some_int = rng.random::<u32>();
//! let uniformly_distributed : u32 = rng.range(0..=42);
//! let a_float : f64 = rng.range(0.0..42.0);
//! }
//! ```
//!
//! `smallrand` can also be used as "no-std", in which case you can use it like this:
//! ```rust
//! use smallrand::{StdRng, SplitMix};
//! const SEED : u64 = 42;
//! let mut rng = StdRng::from_entropy(&mut SplitMix::new(SEED));
//! let some_int = rng.random::<u32>();
//! ```
//!
//! The use of the `SplitMix` may seem cumbersome, but this is done for two reasons:
//! - Requiring that the `StdRng` is initialized from something that implements `EntropySource`
//! (like `SplitMix`) provides an interface independent of the actual algorithm used
//! (different algorithms need different amount of entropy when initializing).
//! - Xoshiro256++ must be initialized with four u64 values. Having that as the interface could
//! tempt users to provide only one actual value and use zero for the other three.
//! This could cause the algorithm to produce very bad output. The use of `SplitMix`
//! generates values that makes most algorithms perform better in this case.
//!
//! It is fairly easy to write your own implementation of `EntropySource` for your platform.
//!
extern crate core;
pub use ChaCha12;
pub use DefaultEntropy;
pub use DevUrandom;
pub use EntropySource;
pub use GetRandom;
pub use HashMapEntropy;
pub use SplitMix;
pub use Rng;
pub use SecureEntropy;
pub use SmallRng;
pub use StdRng;
pub use Xoshiro256pp;