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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright © 2023-2026 vrd. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full
// license information.
// `deny`, not `forbid`, so the optional `simd` module (which needs
// architecture intrinsics) can lift it with a module-local `#[allow]`.
// All other modules must remain free of `unsafe`.
//! # Versatile Random Distributions (VRD)
//!
//! [](https://crates.io/crates/vrd)
//! [](https://docs.rs/vrd)
//! [](https://github.com/sebastienrousseau/vrd#license)
//!
//! A lightweight, `no_std`-friendly random number generator backed by
//! **Xoshiro256++**, with optional **Mersenne Twister (MT19937)** support.
//!
//! ## Features
//! - **High performance:** Xoshiro256++ default — 32-byte state, period
//! 2^256 - 1, SplitMix64 seed whitening.
//! - **Legacy reproducibility:** opt-in MT19937 backend.
//! `Random::new_mersenne_twister()` requires `alloc + std`;
//! `Random::new_mersenne_twister_with_seed(u32)` is `alloc`-only.
//! - **`no_std` ready:** pure-core build with `default-features = false`,
//! validated on `thumbv7em-none-eabihf` (Cortex-M) and
//! `wasm32-unknown-unknown` (WebAssembly) in CI.
//! - **Unbiased sampling:** `int`, `uint`, `random_range`, and the public
//! `bounded` use Lemire's nearly-divisionless method.
//! - **Bit-precise floats:** `float()` carries 24 mantissa bits, `double()`
//! carries 53. Always `[0.0, 1.0)`.
//! - **Distributions:** `uniform(low, high)`, `normal`, `exponential`,
//! `poisson` — `std`-free via `libm`.
//! - **Convenience helpers:** `iter_u32` / `iter_u64` / `iter_bytes`
//! iterator adapters; `uuid_v4_bytes` (`no_std`) and `uuid_v4`
//! (`alloc`); `hex_token` and `base64_token` for URL-safe random
//! tokens.
//! - **`rand 0.10` traits:** `TryRng`, the blanket-implemented `Rng`, and
//! `SeedableRng`.
//!
//! ## Quickstart
//!
//! ```
//! use vrd::Random;
//!
//! let mut rng = Random::from_u64_seed(42); // deterministic, allocation-free
//!
//! let n: u32 = rng.rand(); // any u32
//! let _ = rng.u64(); // any u64
//! let _ = rng.int(1, 100); // i32 in [1, 100], uniform
//! let _ = rng.double(); // f64 in [0.0, 1.0)
//! let _ = rng.bool(0.5); // 50/50 coin
//! assert!(n > 0 || n == 0);
//! ```
//!
//! Use [`Random::new()`] for entropy-seeded randomness on `std` targets,
//! [`Random::from_seed()`] / [`Random::from_u64_seed()`] for deterministic
//! / `no_std` use, and [`Random::new_mersenne_twister()`] / [`Random::new_mersenne_twister_with_seed()`]
//! when you need bit-for-bit MT19937 reproducibility against existing
//! test vectors.
//!
//! ## Choosing a backend
//!
//! Default `Random` is non-cryptographic Xoshiro256++. For
//! credentials, session IDs, or anything an attacker would benefit
//! from predicting, enable the `crypto` feature and construct via
//! [`Random::new_secure`] (entropy-seeded) or
//! [`Random::from_secure_seed`] (deterministic). The other backends
//! cover different speed / state-size / reproducibility points:
//!
//! | Backend | Constructor | State | Crypto-quality? |
//! | :--- | :--- | ---: | :---: |
//! | Xoshiro256++ | [`Random::new`] | 32 B | no |
//! | MT19937 | [`Random::new_mersenne_twister`] | 2 488 B | no |
//! | PCG32 / PCG64 | [`Random::new_pcg32`] / [`Random::new_pcg64`] | 16 / 32 B | no |
//! | ChaCha20 | [`Random::new_secure`] | ~256 B | **yes** |
//!
//! ## Optional features
//!
//! - `simd` — SIMD-batched `fill_bytes` (~2–3× bulk throughput).
//! - `pcg` — PCG32 / PCG64 backends.
//! - `crypto` — ChaCha20 CSPRNG backend.
//! - `quasirandom` — Halton / Sobol / Van der Corput low-discrepancy
//! sequences for Monte Carlo integration.
//! - `serde` — `Serialize` / `Deserialize` on the public types.
extern crate alloc;
extern crate std;
use fmt;
/// Crate-level error type for the `vrd` library.
///
/// This error type is used to represent general failures within the library.
/// It is kept allocation-free by using static error messages, ensuring it
/// works correctly in pure `no_std` environments without requiring an
/// allocator.
///
/// # Examples
///
/// ```
/// use vrd::VrdError;
///
/// let err = VrdError::GeneralError("something went wrong");
/// println!("{}", err);
/// ```
/// ChaCha20 CSPRNG (feature `crypto`).
/// Pluggable `Distribution` trait and built-in samplers.
/// Convenience macros.
/// Mersenne Twister configuration and constants.
/// PCG32 / PCG64 generators (feature `pcg`).
/// Quasi-random low-discrepancy sequences (feature `quasirandom`).
/// The core `Random` facade.
/// Xoshiro256++ implementation.
/// SIMD-batched `fill_bytes` (feature `simd`).
///
/// Architecture-conditional (NEON on aarch64, AVX2 on x86_64);
/// excluded from coverage measurement via `.tarpaulin.toml` because
/// a single-platform tarpaulin run can never observe both halves.
/// Validated by the dedicated `simd` CI matrix job that runs
/// `cargo test --features simd` on both ubuntu-latest and
/// macos-latest.
/// Ziggurat sampler for `Random::normal()`.
pub use Distribution;
pub use ;
pub use ;