Merge branch 'master' into rand_os

This commit is contained in:
Artyom Pavlov
2018-12-27 14:59:34 +00:00
committed by GitHub
61 changed files with 2121 additions and 150 deletions
+6 -4
View File
@@ -71,6 +71,7 @@ matrix:
- cargo test --manifest-path rand_isaac/Cargo.toml --features=serde1
# TODO: cannot test rand_pcg due to explicit dependency on i128
- cargo test --manifest-path rand_xorshift/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xoshiro/Cargo.toml
- cargo test --manifest-path rand_chacha/Cargo.toml
- cargo test --manifest-path rand_hc/Cargo.toml
- cargo test --manifest-path rand_os/Cargo.toml
@@ -91,6 +92,7 @@ matrix:
- cargo test --manifest-path rand_isaac/Cargo.toml --features=serde1
- cargo test --manifest-path rand_pcg/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xorshift/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xoshiro/Cargo.toml
- cargo test --manifest-path rand_chacha/Cargo.toml
- cargo test --manifest-path rand_hc/Cargo.toml
- cargo test --manifest-path rand_os/Cargo.toml
@@ -117,6 +119,7 @@ matrix:
- cargo test --manifest-path rand_isaac/Cargo.toml --features=serde1
- cargo test --manifest-path rand_pcg/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xorshift/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xoshiro/Cargo.toml
- cargo test --manifest-path rand_chacha/Cargo.toml
- cargo test --manifest-path rand_hc/Cargo.toml
- cargo test --manifest-path rand_os/Cargo.toml
@@ -132,10 +135,8 @@ matrix:
install:
- rustup target add wasm32-unknown-unknown
- rustup target add wasm32-unknown-emscripten
# cargo-web takes ±10 min. to install, and cargo and cargo-update offer
# no reliable update-or-install command. We ignore updating for now
# (just drop the Travis' caches when necessary)
- cargo --list | egrep "^\s*web$" -q || cargo install cargo-web
- nvm install 9
- ./utils/ci/install_cargo_web.sh
- cargo web -V
addons:
chrome: stable
@@ -201,6 +202,7 @@ script:
- cargo test --manifest-path rand_isaac/Cargo.toml --features=serde1
- cargo test --manifest-path rand_pcg/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xorshift/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xoshiro/Cargo.toml
- cargo test --manifest-path rand_chacha/Cargo.toml
- cargo test --manifest-path rand_hc/Cargo.toml
- cargo test --manifest-path rand_os/Cargo.toml
+4
View File
@@ -9,6 +9,10 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.
## [0.6.1] - 2018-11-22
- Support sampling `Duration` also for `no_std` (only since Rust 1.25) (#649)
- Disable default features of `libc` (#647)
## [0.6.0] - 2018-11-14
### Project organisation
+4 -3
View File
@@ -1,11 +1,11 @@
[package]
name = "rand"
version = "0.6.0" # NB: When modifying, also modify html_root_url in lib.rs
version = "0.6.1"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand"
documentation = "https://rust-random.github.io/rand"
homepage = "https://crates.io/crates/rand"
description = """
Random number generators and other randomness functionality.
@@ -40,6 +40,7 @@ rand_isaac = { path = "rand_isaac", version = "0.1" }
rand_chacha = { path = "rand_chacha", version = "0.1" }
rand_hc = { path = "rand_hc", version = "0.1" }
rand_xorshift = { path = "rand_xorshift", version = "0.1" }
rand_xoshiro = { path = "rand_xoshiro", version = "0.1" }
log = { version = "0.4", optional = true }
[dependencies.packed_simd]
@@ -61,7 +62,7 @@ winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", "
average = "0.9.2"
[build-dependencies]
rustc_version = "0.2"
autocfg = "0.1"
[package.metadata.docs.rs]
all-features = true
+9 -8
View File
@@ -2,10 +2,11 @@
[![Build Status](https://travis-ci.org/rust-random/rand.svg?branch=master)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand.svg)](https://crates.io/crates/rand)
[![Documentation](https://docs.rs/rand/badge.svg)](https://docs.rs/rand)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand.svg)](https://github.com/rust-random/rand#license)
[![Crate](https://img.shields.io/crates/v/rand.svg)](https://crates.io/crates/rand)
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand)
[![API](https://docs.rs/rand/badge.svg)](https://docs.rs/rand)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
A Rust library for random number generation.
@@ -18,9 +19,9 @@ implementations should prefer to use `rand_core` while most other users should
depend on `rand`.
Documentation:
- [The Rust Rand Book](https://rust-random.github.io/book/)
- [API reference for the latest release](https://docs.rs/rand/)
- [API reference for the master branch](https://rust-random.github.io/rand/)
- [The Rust Rand Book](https://rust-random.github.io/book)
- [API reference (master)](https://rust-random.github.io/rand)
- [API reference (docs.rs)](https://docs.rs/rand)
## Usage
@@ -32,7 +33,7 @@ Add this to your `Cargo.toml`:
rand = "0.6"
```
To get started using Rand, see [The Book](https://rust-random.github.io/book/).
To get started using Rand, see [The Book](https://rust-random.github.io/book).
## Versions
+2
View File
@@ -41,6 +41,7 @@ test_script:
- cargo test --package rand_core --no-default-features --features=alloc
- cargo test --package rand_isaac --features=serde1
- cargo test --package rand_xorshift --features=serde1
- cargo test --package rand_xoshiro
- cargo test --package rand_chacha
- cargo test --package rand_hc
- cargo test --manifest-path rand_core/Cargo.toml
@@ -48,6 +49,7 @@ test_script:
- cargo test --manifest-path rand_isaac/Cargo.toml --features=serde1
- cargo test --manifest-path rand_pcg/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xorshift/Cargo.toml --features=serde1
- cargo test --manifest-path rand_xoshiro/Cargo.toml
- cargo test --manifest-path rand_chacha/Cargo.toml
- cargo test --manifest-path rand_hc/Cargo.toml
- cargo test --manifest-path rand_os/Cargo.toml
+22
View File
@@ -101,6 +101,26 @@ macro_rules! distr {
}
}
macro_rules! distr_arr {
($fnn:ident, $ty:ty, $distr:expr) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = SmallRng::from_entropy();
let distr = $distr;
b.iter(|| {
let mut accum = 0u32;
for _ in 0..::RAND_BENCH_N {
let x: $ty = distr.sample(&mut rng);
accum = accum.wrapping_add(x[0] as u32);
}
accum
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
}
}
// uniform
distr_int!(distr_uniform_i8, i8, Uniform::new(20i8, 100));
distr_int!(distr_uniform_i16, i16, Uniform::new(-500i16, 2000));
@@ -158,6 +178,8 @@ distr_float!(distr_cauchy, f64, Cauchy::new(4.2, 6.9));
distr_int!(distr_binomial, u64, Binomial::new(20, 0.7));
distr_int!(distr_poisson, u64, Poisson::new(4.0));
distr!(distr_bernoulli, bool, Bernoulli::new(0.18));
distr_arr!(distr_circle, [f64; 2], UnitCircle::new());
distr_arr!(distr_sphere_surface, [f64; 3], UnitSphereSurface::new());
// Weighted
distr_int!(distr_weighted_i8, usize, WeightedIndex::new(&[1i8, 2, 3, 4, 12, 0, 2, 1]).unwrap());
+40
View File
@@ -15,6 +15,7 @@ extern crate rand_chacha;
extern crate rand_hc;
extern crate rand_pcg;
extern crate rand_xorshift;
extern crate rand_xoshiro;
const RAND_BENCH_N: u64 = 1000;
const BYTES_LEN: usize = 1024;
@@ -30,6 +31,9 @@ use rand_chacha::ChaChaRng;
use rand_hc::{Hc128Rng, Hc128Core};
use rand_pcg::{Lcg64Xsh32, Mcg128Xsl64};
use rand_xorshift::XorShiftRng;
use rand_xoshiro::{Xoshiro256StarStar, Xoshiro256Plus, Xoshiro128StarStar,
Xoshiro128Plus, Xoroshiro128StarStar, Xoroshiro128Plus, SplitMix64,
Xoroshiro64StarStar, Xoroshiro64Star};
macro_rules! gen_bytes {
($fnn:ident, $gen:expr) => {
@@ -49,6 +53,15 @@ macro_rules! gen_bytes {
}
gen_bytes!(gen_bytes_xorshift, XorShiftRng::from_entropy());
gen_bytes!(gen_bytes_xoshiro256starstar, Xoshiro256StarStar::from_entropy());
gen_bytes!(gen_bytes_xoshiro256plus, Xoshiro256Plus::from_entropy());
gen_bytes!(gen_bytes_xoshiro128starstar, Xoshiro128StarStar::from_entropy());
gen_bytes!(gen_bytes_xoshiro128plus, Xoshiro128Plus::from_entropy());
gen_bytes!(gen_bytes_xoroshiro128starstar, Xoroshiro128StarStar::from_entropy());
gen_bytes!(gen_bytes_xoroshiro128plus, Xoroshiro128Plus::from_entropy());
gen_bytes!(gen_bytes_xoroshiro64starstar, Xoroshiro64StarStar::from_entropy());
gen_bytes!(gen_bytes_xoroshiro64star, Xoroshiro64Star::from_entropy());
gen_bytes!(gen_bytes_splitmix64, SplitMix64::from_entropy());
gen_bytes!(gen_bytes_lcg64_xsh32, Lcg64Xsh32::from_entropy());
gen_bytes!(gen_bytes_mcg128_xsh64, Mcg128Xsl64::from_entropy());
gen_bytes!(gen_bytes_chacha20, ChaChaRng::from_entropy());
@@ -77,6 +90,15 @@ macro_rules! gen_uint {
}
gen_uint!(gen_u32_xorshift, u32, XorShiftRng::from_entropy());
gen_uint!(gen_u32_xoshiro256starstar, u32, Xoshiro256StarStar::from_entropy());
gen_uint!(gen_u32_xoshiro256plus, u32, Xoshiro256Plus::from_entropy());
gen_uint!(gen_u32_xoshiro128starstar, u32, Xoshiro128StarStar::from_entropy());
gen_uint!(gen_u32_xoshiro128plus, u32, Xoshiro128Plus::from_entropy());
gen_uint!(gen_u32_xoroshiro128starstar, u32, Xoroshiro128StarStar::from_entropy());
gen_uint!(gen_u32_xoroshiro128plus, u32, Xoroshiro128Plus::from_entropy());
gen_uint!(gen_u32_xoroshiro64starstar, u32, Xoroshiro64StarStar::from_entropy());
gen_uint!(gen_u32_xoroshiro64star, u32, Xoroshiro64Star::from_entropy());
gen_uint!(gen_u32_splitmix64, u32, SplitMix64::from_entropy());
gen_uint!(gen_u32_lcg64_xsh32, u32, Lcg64Xsh32::from_entropy());
gen_uint!(gen_u32_mcg128_xsh64, u32, Mcg128Xsl64::from_entropy());
gen_uint!(gen_u32_chacha20, u32, ChaChaRng::from_entropy());
@@ -88,6 +110,15 @@ gen_uint!(gen_u32_small, u32, SmallRng::from_entropy());
gen_uint!(gen_u32_os, u32, OsRng::new().unwrap());
gen_uint!(gen_u64_xorshift, u64, XorShiftRng::from_entropy());
gen_uint!(gen_u64_xoshiro256starstar, u64, Xoshiro256StarStar::from_entropy());
gen_uint!(gen_u64_xoshiro256plus, u64, Xoshiro256Plus::from_entropy());
gen_uint!(gen_u64_xoshiro128starstar, u64, Xoshiro128StarStar::from_entropy());
gen_uint!(gen_u64_xoshiro128plus, u64, Xoshiro128Plus::from_entropy());
gen_uint!(gen_u64_xoroshiro128starstar, u64, Xoroshiro128StarStar::from_entropy());
gen_uint!(gen_u64_xoroshiro128plus, u64, Xoroshiro128Plus::from_entropy());
gen_uint!(gen_u64_xoroshiro64starstar, u64, Xoroshiro64StarStar::from_entropy());
gen_uint!(gen_u64_xoroshiro64star, u64, Xoroshiro64Star::from_entropy());
gen_uint!(gen_u64_splitmix64, u64, SplitMix64::from_entropy());
gen_uint!(gen_u64_lcg64_xsh32, u64, Lcg64Xsh32::from_entropy());
gen_uint!(gen_u64_mcg128_xsh64, u64, Mcg128Xsl64::from_entropy());
gen_uint!(gen_u64_chacha20, u64, ChaChaRng::from_entropy());
@@ -123,6 +154,15 @@ macro_rules! init_gen {
}
init_gen!(init_xorshift, XorShiftRng);
init_gen!(init_xoshiro256starstar, Xoshiro256StarStar);
init_gen!(init_xoshiro256plus, Xoshiro256Plus);
init_gen!(init_xoshiro128starstar, Xoshiro128StarStar);
init_gen!(init_xoshiro128plus, Xoshiro128Plus);
init_gen!(init_xoroshiro128starstar, Xoroshiro128StarStar);
init_gen!(init_xoroshiro128plus, Xoroshiro128Plus);
init_gen!(init_xoroshiro64starstar, Xoroshiro64StarStar);
init_gen!(init_xoroshiro64star, Xoroshiro64Star);
init_gen!(init_splitmix64, SplitMix64);
init_gen!(init_lcg64_xsh32, Lcg64Xsh32);
init_gen!(init_mcg128_xsh64, Mcg128Xsl64);
init_gen!(init_hc128, Hc128Rng);
+7 -8
View File
@@ -1,11 +1,10 @@
extern crate rustc_version;
use rustc_version::{version, Version};
extern crate autocfg;
fn main() {
if version().unwrap() >= Version::parse("1.26.0").unwrap() {
println!("cargo:rustc-cfg=rust_1_26");
}
if version().unwrap() >= Version::parse("1.27.0").unwrap() {
println!("cargo:rustc-cfg=rust_1_27");
}
println!("cargo:rerun-if-changed=build.rs");
let ac = autocfg::new();
ac.emit_rustc_version(1, 25);
ac.emit_rustc_version(1, 26);
ac.emit_rustc_version(1, 27);
}
+3 -3
View File
@@ -1,11 +1,11 @@
[package]
name = "rand_chacha"
version = "0.1.0" # NB: When modifying, also modify html_root_url in lib.rs
version = "0.1.0"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand_chacha"
documentation = "https://rust-random.github.io/rand/rand_chacha"
homepage = "https://crates.io/crates/rand_chacha"
description = """
ChaCha random number generator
@@ -22,4 +22,4 @@ appveyor = { repository = "rust-random/rand" }
rand_core = { path = "../rand_core", version = ">=0.2, <0.4", default-features=false }
[build-dependencies]
rustc_version = "0.2"
autocfg = "0.1"
+8 -7
View File
@@ -3,9 +3,10 @@
[![Build Status](https://travis-ci.org/rust-random/rand.svg)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_chacha.svg)](https://crates.io/crates/rand_chacha)
[![Documentation](https://docs.rs/rand_chacha/badge.svg)](https://docs.rs/rand_chacha)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand_chacha.svg)](https://github.com/rust-random/rand/tree/master/rand_chacha#license)
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_chacha)
[![API](https://docs.rs/rand_chacha/badge.svg)](https://docs.rs/rand_chacha)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
A cryptographically secure random number generator that uses the ChaCha
algorithm.
@@ -15,11 +16,11 @@ as an RNG. It is an improved variant of the Salsa20 cipher family, which was
selected as one of the "stream ciphers suitable for widespread adoption" by
eSTREAM[^2].
Documentation:
[master branch](https://rust-random.github.io/rand/rand_chacha/index.html),
[by release](https://docs.rs/rand_chacha)
Links:
[Changelog](CHANGELOG.md)
- [API documentation (master)](https://rust-random.github.io/rand/rand_chacha)
- [API documentation (docs.rs)](https://docs.rs/rand_chacha)
- [Changelog](CHANGELOG.md)
[rand]: https://crates.io/crates/rand
[^1]: D. J. Bernstein, [*ChaCha, a variant of Salsa20*](
+4 -5
View File
@@ -1,8 +1,7 @@
extern crate rustc_version;
use rustc_version::{version, Version};
extern crate autocfg;
fn main() {
if version().unwrap() >= Version::parse("1.26.0").unwrap() {
println!("cargo:rustc-cfg=rust_1_26");
}
println!("cargo:rerun-if-changed=build.rs");
let ac = autocfg::new();
ac.emit_rustc_version(1, 26);
}
+3 -3
View File
@@ -114,7 +114,7 @@ impl ChaChaRng {
/// byte-offset.
///
/// Note: this function is currently only available with Rust 1.26 or later.
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
pub fn get_word_pos(&self) -> u128 {
let mut c = (self.0.core.state[13] as u64) << 32
| (self.0.core.state[12] as u64);
@@ -135,7 +135,7 @@ impl ChaChaRng {
/// 60 bits.
///
/// Note: this function is currently only available with Rust 1.26 or later.
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
pub fn set_word_pos(&mut self, word_offset: u128) {
let index = (word_offset as usize) & 0xF;
let counter = (word_offset >> 4) as u64;
@@ -330,7 +330,7 @@ mod test {
}
#[test]
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
fn test_chacha_true_values_c() {
// Test vector 4 from
// https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
+1 -1
View File
@@ -10,7 +10,7 @@
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_chacha/0.1.0")]
html_root_url = "https://rust-random.github.io/rand/")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
+2 -2
View File
@@ -1,11 +1,11 @@
[package]
name = "rand_core"
version = "0.3.0" # NB: When modifying, also modify html_root_url in lib.rs
version = "0.3.0"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand_core"
documentation = "https://rust-random.github.io/rand/rand_core"
homepage = "https://crates.io/crates/rand_core"
description = """
Core random number generator traits and tools for implementation.
+8 -7
View File
@@ -3,9 +3,10 @@
[![Build Status](https://travis-ci.org/rust-random/rand.svg)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_core.svg)](https://crates.io/crates/rand_core)
[![Documentation](https://docs.rs/rand_core/badge.svg)](https://docs.rs/rand_core)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand_core.svg)](https://github.com/rust-random/rand/tree/master/rand_core#license)
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_core)
[![API](https://docs.rs/rand_core/badge.svg)](https://docs.rs/rand_core)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
Core traits and error types of the [rand] library, plus tools for implementing
RNGs.
@@ -20,11 +21,11 @@ applications (including sampling from restricted ranges, conversion to floating
point, list permutations and secure initialisation of RNGs). Most users should
prefer to use the main [rand] crate.
Documentation:
[master branch](https://rust-random.github.io/rand/rand_core/index.html),
[by release](https://docs.rs/rand_core)
Links:
[Changelog](CHANGELOG.md)
- [API documentation (master)](https://rust-random.github.io/rand/rand_core)
- [API documentation (docs.rs)](https://docs.rs/rand_core)
- [Changelog](CHANGELOG.md)
[rand]: https://crates.io/crates/rand
+1 -1
View File
@@ -34,7 +34,7 @@
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_core/0.3.0")]
html_root_url = "https://rust-random.github.io/rand/")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
+2 -2
View File
@@ -1,11 +1,11 @@
[package]
name = "rand_hc"
version = "0.1.0" # NB: When modifying, also modify html_root_url in lib.rs
version = "0.1.0"
authors = ["The Rand Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand_hc"
documentation = "https://rust-random.github.io/rand/rand_hc"
homepage = "https://crates.io/crates/rand_hc"
description = """
HC128 random number generator
+8 -7
View File
@@ -3,9 +3,10 @@
[![Build Status](https://travis-ci.org/rust-random/rand.svg)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_hc.svg)](https://crates.io/crates/rand_hc)
[![Documentation](https://docs.rs/rand_hc/badge.svg)](https://docs.rs/rand_hc)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand_hc.svg)](https://github.com/rust-random/rand/tree/master/rand_hc#license)
[[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_hc)
[![API](https://docs.rs/rand_hc/badge.svg)](https://docs.rs/rand_hc)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
A cryptographically secure random number generator that uses the HC-128
algorithm.
@@ -14,11 +15,11 @@ HC-128 is a stream cipher designed by Hongjun Wu[^1], that we use as an
RNG. It is selected as one of the "stream ciphers suitable for widespread
adoption" by eSTREAM[^2].
Documentation:
[master branch](https://rust-random.github.io/rand/rand_hc/index.html),
[by release](https://docs.rs/rand_hc)
Links:
[Changelog](CHANGELOG.md)
- [API documentation (master)](https://rust-random.github.io/rand/rand_hc)
- [API documentation (docs.rs)](https://docs.rs/rand_hc)
- [Changelog](CHANGELOG.md)
[rand]: https://crates.io/crates/rand
[^1]: Hongjun Wu (2008). ["The Stream Cipher HC-128"](
+1 -1
View File
@@ -10,7 +10,7 @@
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_hc/0.1.0")]
html_root_url = "https://rust-random.github.io/rand/")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
+4
View File
@@ -4,5 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.1.1] - 2018-11-26
- Fix `rand_core` version requirement
- Fix doc links
## [0.1.0] - 2018-10-17
- Pulled out of the Rand crate
+3 -3
View File
@@ -1,11 +1,11 @@
[package]
name = "rand_isaac"
version = "0.1.0" # NB: When modifying, also modify html_root_url in lib.rs
version = "0.1.1"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand_isaac"
documentation = "https://rust-random.github.io/rand/rand_isaac"
homepage = "https://crates.io/crates/rand_isaac"
description = """
ISAAC random number generator
@@ -21,7 +21,7 @@ appveyor = { repository = "rust-random/rand" }
serde1 = ["serde", "serde_derive", "rand_core/serde1"]
[dependencies]
rand_core = { path = "../rand_core", version = ">=0.2, <0.4", default-features=false }
rand_core = { path = "../rand_core", version = "0.3", default-features=false }
serde = { version = "1", optional = true }
serde_derive = { version = "^1.0.38", optional = true }
+8 -8
View File
@@ -3,9 +3,10 @@
[![Build Status](https://travis-ci.org/rust-random/rand.svg)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_isaac.svg)](https://crates.io/crates/rand_isaac)
[![Documentation](https://docs.rs/rand_isaac/badge.svg)](https://docs.rs/rand_isaac)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand_isaac.svg)](https://github.com/rust-random/rand/tree/master/rand_isaac#license)
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_isaac)
[![API](https://docs.rs/rand_isaac/badge.svg)](https://docs.rs/rand_isaac)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
Implements the ISAAC and ISAAC-64 random number generators.
@@ -17,14 +18,13 @@ in 1996[^1][^2].
ISAAC is notably fast and produces excellent quality random numbers for
non-cryptographic applications.
Documentation:
[master branch](https://rust-random.github.io/rand/rand_isaac/index.html),
[by release](https://docs.rs/rand_isaac)
Links:
[Changelog](CHANGELOG.md)
- [API documentation (master)](https://rust-random.github.io/rand/rand_isaac)
- [API documentation (docs.rs)](https://docs.rs/rand_isaac)
- [Changelog](CHANGELOG.md)
[rand]: https://crates.io/crates/rand
[^1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number generator*](http://burtleburtle.net/bob/rand/isaacafa.html)
[^2]: Bob Jenkins, [*ISAAC and RC4*](http://burtleburtle.net/bob/rand/isaac.html)
+1 -1
View File
@@ -10,7 +10,7 @@
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_isaac/0.1.0")]
html_root_url = "https://rust-random.github.io/rand/")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
+3 -3
View File
@@ -1,11 +1,11 @@
[package]
name = "rand_pcg"
version = "0.1.1" # NB: When modifying, also modify html_root_url in lib.rs
version = "0.1.1"
authors = ["The Rand Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand_pcg"
documentation = "https://rust-random.github.io/rand/rand_pcg"
homepage = "https://crates.io/crates/rand_pcg"
description = """
Selected PCG random number generators
@@ -34,4 +34,4 @@ serde_derive = { version = "^1.0.38", optional = true }
bincode = { version = "1", features = ["i128"] }
[build-dependencies]
rustc_version = "0.2"
autocfg = "0.1"
+8 -7
View File
@@ -3,9 +3,10 @@
[![Build Status](https://travis-ci.org/rust-random/rand.svg?branch=master)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_pcg.svg)](https://crates.io/crates/rand_pcg)
[![Documentation](https://docs.rs/rand_pcg/badge.svg)](https://docs.rs/rand_pcg)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand_pcg.svg)](https://github.com/rust-random/rand/tree/master/rand_pcg#license)
[[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_pcg)
[![API](https://docs.rs/rand_pcg/badge.svg)](https://docs.rs/rand_pcg)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
Implements a selection of PCG random number generators.
@@ -19,11 +20,11 @@ See the [pcg-random website](http://www.pcg-random.org/).
This crate depends on [rand_core](https://crates.io/crates/rand_core) and is
part of the [Rand project](https://github.com/rust-random/rand).
Documentation:
[master branch](https://rust-random.github.io/rand/rand_pcg/index.html),
[by release](https://docs.rs/rand_pcg)
Links:
[Changelog](CHANGELOG.md)
- [API documentation (master)](https://rust-random.github.io/rand/rand_pcg)
- [API documentation (docs.rs)](https://docs.rs/rand_pcg)
- [Changelog](CHANGELOG.md)
## Crate Features
+4 -5
View File
@@ -1,8 +1,7 @@
extern crate rustc_version;
use rustc_version::{version, Version};
extern crate autocfg;
fn main() {
if version().unwrap() >= Version::parse("1.26.0").unwrap() {
println!("cargo:rustc-cfg=rust_1_26");
}
println!("cargo:rerun-if-changed=build.rs");
let ac = autocfg::new();
ac.emit_rustc_version(1, 26);
}
+3 -3
View File
@@ -29,7 +29,7 @@
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_pcg/0.1.1")]
html_root_url = "https://rust-random.github.io/rand/")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
@@ -42,7 +42,7 @@ extern crate rand_core;
#[cfg(feature="serde1")] #[macro_use] extern crate serde_derive;
mod pcg64;
#[cfg(rust_1_26)] mod pcg128;
#[cfg(rustc_1_26)] mod pcg128;
pub use self::pcg64::{Pcg32, Lcg64Xsh32};
#[cfg(rust_1_26)] pub use self::pcg128::{Pcg64Mcg, Mcg128Xsl64};
#[cfg(rustc_1_26)] pub use self::pcg128::{Pcg64Mcg, Mcg128Xsl64};
+1 -1
View File
@@ -1,4 +1,4 @@
#![cfg(rust_1_26)]
#![cfg(rustc_1_26)]
extern crate rand_pcg;
extern crate rand_core;
#[cfg(all(feature="serde1", test))] extern crate bincode;
+2 -2
View File
@@ -1,11 +1,11 @@
[package]
name = "rand_xorshift"
version = "0.1.0" # NB: When modifying, also modify html_root_url in lib.rs
version = "0.1.0"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand_xorshift"
documentation = "https://rust-random.github.io/rand/rand_xorshift"
homepage = "https://crates.io/crates/rand_xorshift"
description = """
Xorshift random number generator
+8 -7
View File
@@ -3,9 +3,10 @@
[![Build Status](https://travis-ci.org/rust-random/rand.svg)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_xorshift.svg)](https://crates.io/crates/rand_xorshift)
[![Documentation](https://docs.rs/rand_xorshift/badge.svg)](https://docs.rs/rand_xorshift)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand_xorshift.svg)](https://github.com/rust-random/rand/tree/master/rand_xorshift#license)
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_xorshift)
[![API](https://docs.rs/rand_xorshift/badge.svg)](https://docs.rs/rand_xorshift)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
Implements the Xorshift random number generator.
@@ -17,11 +18,11 @@ requirements, use a more secure one such as `StdRng` or `OsRng`.
["Xorshift RNGs"](https://www.jstatsoft.org/v08/i14/paper).
*Journal of Statistical Software*. Vol. 8 (Issue 14).
Documentation:
[master branch](https://rust-random.github.io/rand/rand_xorshift/index.html),
[by release](https://docs.rs/rand_xorshift)
Links:
[Changelog](CHANGELOG.md)
- [API documentation (master)](https://rust-random.github.io/rand/rand_xorshift)
- [API documentation (docs.rs)](https://docs.rs/rand_xorshift)
- [Changelog](CHANGELOG.md)
[rand]: https://crates.io/crates/rand
+1 -1
View File
@@ -10,7 +10,7 @@
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_xorshift/0.1.0")]
html_root_url = "https://rust-random.github.io/rand/")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
+8
View File
@@ -0,0 +1,8 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.1.0] - 2018-11-??
Initial release.
+19
View File
@@ -0,0 +1,19 @@
[package]
name = "rand_xoshiro"
version = "0.1.0" # NB: When modifying, also modify html_root_url in lib.rs
authors = ["The Rand Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-random/rand"
documentation = "https://docs.rs/rand_xoshiro"
homepage = "https://crates.io/crates/rand_xoshiro"
description = "Xoshiro, xoroshiro and splitmix64 random number generators"
keywords = ["random", "rng"]
categories = ["algorithms"]
[dependencies]
byteorder = "1"
rand_core = { path = "../rand_core", version = "0.3", default-features=false }
[dev-dependencies]
rand = { path = "..", default-features=false } # needed for doctests
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+25
View File
@@ -0,0 +1,25 @@
Copyright (c) 2018 Developers of the Rand project
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
+27
View File
@@ -0,0 +1,27 @@
# rand_xoshiro
[![Build Status](https://travis-ci.org/rust-random/rand.svg?branch=master)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_xoshiro.svg)](https://crates.io/crates/rand_xoshiro)
[![Documentation](https://docs.rs/rand_xoshiro/badge.svg)](https://docs.rs/rand_xoshiro)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-random/rand#rust-version-requirements)
[![License](https://img.shields.io/crates/l/rand_xoshiro.svg)](https://github.com/rust-random/rand/tree/master/rand_xoshiro#license)
Rust implementation of the [xoshiro, xoroshiro and splitmix64](http://xoshiro.di.unimi.it) random number generators.
This crate depends on [rand_core](https://crates.io/crates/rand_core) and is
part of the [Rand project](https://github.com/rust-random/rand).
Documentation:
[master branch](https://rust-random.github.io/rand/rand_xoshiro/index.html),
[by release](https://docs.rs/rand_xoshiro)
[Changelog](CHANGELOG.md)
## License
`rand_xoshiro` is distributed under the terms of both the MIT license and the
Apache License (Version 2.0).
See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT), and
[COPYRIGHT](COPYRIGHT) for details.
+234
View File
@@ -0,0 +1,234 @@
/// Initialize a RNG from a `u64` seed using `SplitMix64`.
macro_rules! from_splitmix {
($seed:expr) => { {
let mut rng = ::SplitMix64::seed_from_u64($seed);
Self::from_rng(&mut rng).unwrap()
} }
}
/// Apply the ** scrambler used by some RNGs from the xoshiro family.
macro_rules! starstar_u64 {
($x:expr) => {
$x.wrapping_mul(5).rotate_left(7).wrapping_mul(9)
}
}
/// Apply the ** scrambler used by some RNGs from the xoshiro family.
macro_rules! starstar_u32 {
($x:expr) => {
$x.wrapping_mul(0x9E3779BB).rotate_left(5).wrapping_mul(5)
}
}
/// Implement a jump function for an RNG from the xoshiro family.
macro_rules! impl_jump {
(u32, $self:expr, [$j0:expr, $j1:expr]) => {
const JUMP: [u32; 2] = [$j0, $j1];
let mut s0 = 0;
let mut s1 = 0;
for j in &JUMP {
for b in 0..32 {
if (j & 1 << b) != 0 {
s0 ^= $self.s0;
s1 ^= $self.s1;
}
$self.next_u32();
}
}
$self.s0 = s0;
$self.s1 = s1;
};
(u64, $self:expr, [$j0:expr, $j1:expr]) => {
const JUMP: [u64; 2] = [$j0, $j1];
let mut s0 = 0;
let mut s1 = 0;
for j in &JUMP {
for b in 0..64 {
if (j & 1 << b) != 0 {
s0 ^= $self.s0;
s1 ^= $self.s1;
}
$self.next_u64();
}
}
$self.s0 = s0;
$self.s1 = s1;
};
(u32, $self:expr, [$j0:expr, $j1:expr, $j2:expr, $j3:expr]) => {
const JUMP: [u32; 4] = [$j0, $j1, $j2, $j3];
let mut s0 = 0;
let mut s1 = 0;
let mut s2 = 0;
let mut s3 = 0;
for j in &JUMP {
for b in 0..32 {
if (j & 1 << b) != 0 {
s0 ^= $self.s[0];
s1 ^= $self.s[1];
s2 ^= $self.s[2];
s3 ^= $self.s[3];
}
$self.next_u32();
}
}
$self.s[0] = s0;
$self.s[1] = s1;
$self.s[2] = s2;
$self.s[3] = s3;
};
(u64, $self:expr, [$j0:expr, $j1:expr, $j2:expr, $j3:expr]) => {
const JUMP: [u64; 4] = [$j0, $j1, $j2, $j3];
let mut s0 = 0;
let mut s1 = 0;
let mut s2 = 0;
let mut s3 = 0;
for j in &JUMP {
for b in 0..64 {
if (j & 1 << b) != 0 {
s0 ^= $self.s[0];
s1 ^= $self.s[1];
s2 ^= $self.s[2];
s3 ^= $self.s[3];
}
$self.next_u64();
}
}
$self.s[0] = s0;
$self.s[1] = s1;
$self.s[2] = s2;
$self.s[3] = s3;
};
(u64, $self:expr, [$j0:expr, $j1:expr, $j2:expr, $j3:expr,
$j4:expr, $j5:expr, $j6:expr, $j7:expr]) => {
const JUMP: [u64; 8] = [$j0, $j1, $j2, $j3, $j4, $j5, $j6, $j7];
let mut s = [0; 8];
for j in &JUMP {
for b in 0..64 {
if (j & 1 << b) != 0 {
s[0] ^= $self.s[0];
s[1] ^= $self.s[1];
s[2] ^= $self.s[2];
s[3] ^= $self.s[3];
s[4] ^= $self.s[4];
s[5] ^= $self.s[5];
s[6] ^= $self.s[6];
s[7] ^= $self.s[7];
}
$self.next_u64();
}
}
$self.s = s;
};
}
/// Implement the xoroshiro iteration.
macro_rules! impl_xoroshiro_u32 {
($self:expr) => {
$self.s1 ^= $self.s0;
$self.s0 = $self.s0.rotate_left(26) ^ $self.s1 ^ ($self.s1 << 9);
$self.s1 = $self.s1.rotate_left(13);
}
}
/// Implement the xoroshiro iteration.
macro_rules! impl_xoroshiro_u64 {
($self:expr) => {
$self.s1 ^= $self.s0;
$self.s0 = $self.s0.rotate_left(24) ^ $self.s1 ^ ($self.s1 << 16);
$self.s1 = $self.s1.rotate_left(37);
}
}
/// Implement the xoshiro iteration for `u32` output.
macro_rules! impl_xoshiro_u32 {
($self:expr) => {
let t = $self.s[1] << 9;
$self.s[2] ^= $self.s[0];
$self.s[3] ^= $self.s[1];
$self.s[1] ^= $self.s[2];
$self.s[0] ^= $self.s[3];
$self.s[2] ^= t;
$self.s[3] = $self.s[3].rotate_left(11);
}
}
/// Implement the xoshiro iteration for `u64` output.
macro_rules! impl_xoshiro_u64 {
($self:expr) => {
let t = $self.s[1] << 17;
$self.s[2] ^= $self.s[0];
$self.s[3] ^= $self.s[1];
$self.s[1] ^= $self.s[2];
$self.s[0] ^= $self.s[3];
$self.s[2] ^= t;
$self.s[3] = $self.s[3].rotate_left(45);
}
}
/// Implement the large-state xoshiro iteration.
macro_rules! impl_xoshiro_large {
($self:expr) => {
let t = $self.s[1] << 11;
$self.s[2] ^= $self.s[0];
$self.s[5] ^= $self.s[1];
$self.s[1] ^= $self.s[2];
$self.s[7] ^= $self.s[3];
$self.s[3] ^= $self.s[4];
$self.s[4] ^= $self.s[5];
$self.s[0] ^= $self.s[6];
$self.s[6] ^= $self.s[7];
$self.s[6] ^= t;
$self.s[7] = $self.s[7].rotate_left(21);
}
}
/// Map an all-zero seed to a different one.
macro_rules! deal_with_zero_seed {
($seed:expr, $Self:ident) => {
if $seed.iter().all(|&x| x == 0) {
return $Self::seed_from_u64(0);
}
}
}
/// 512-bit seed for a generator.
///
/// This wrapper is necessary, because some traits required for a seed are not
/// implemented on large arrays.
#[derive(Clone)]
pub struct Seed512(pub [u8; 64]);
impl Seed512 {
/// Return an iterator over the seed.
pub fn iter(&self) -> ::std::slice::Iter<u8> {
self.0.iter()
}
}
impl ::std::fmt::Debug for Seed512 {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.0[..].fmt(f)
}
}
impl Default for Seed512 {
fn default() -> Seed512 {
Seed512([0; 64])
}
}
impl AsMut<[u8]> for Seed512 {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
+98
View File
@@ -0,0 +1,98 @@
//! This crate implements the [xoshiro] family of pseudorandom number generators
//! designed by David Blackman and Sebastiano Vigna. They feature high
//! perfomance and a small state and superseed the previous xorshift-based
//! generators. However, they are no cryptographically secure and their output
//! can be predicted by observing a few samples.
//!
//! The following generators are implemented:
//!
//! # 64-bit generators
//! - [`Xoshiro256StarStar`]: Recommended for all purposes. Excellent speed and
//! a state space (256 bits) large enough for any parallel application.
//! - [`Xoshiro256Plus`]: Recommended for generating 64-bit floating-point
//! numbers. About 15% faster than `Xoshiro256StarStar`, but has a [low linear
//! complexity] in the lowest bits (which are discarded when generating
//! floats), making it fail linearity tests. This is unlikely to have any
//! impact in practise.
//! - [`Xoroshiro128StarStar`]: An alternative to `Xoshiro256StarStar`, having
//! the same speed but using half the state. Only suited for low-scale parallel
//! applications.
//! - [`Xoroshiro128Plus`]: An alternative to `Xoshiro256Plus`, having the same
//! speed but using half the state. Only suited for low-scale parallel
//! applications. Has a [low linear complexity] in the lowest bits (which are
//! discarded when generating floats), making it fail linearity tests. This is
//! unlikely to have any impact in practise.
//! - [`Xoshiro512StarStar`]: An alternative to `Xoshiro256StarStar` with more
//! state and the same speed.
//! - [`Xoshiro512Plus`]: An alternative to `Xoshiro512Plus` with more
//! state and the same speed. Has a [low linear complexity] in the lowest bits
//! (which are discarded when generating floats), making it fail linearity
//! tests. This is unlikely to have any impact in practise.
//! - [`SplitMix64`]: Recommended for initializing generators of the xoshiro
//! familiy from a 64-bit seed. Used for implementing `seed_from_u64`.
//!
//! # 32-bit generators
//! - [`Xoshiro128StarStar`]: Recommended for all purposes. Excellent speed.
//! - [`Xoshiro128Plus`]: Recommended for generating 32-bit floating-point
//! numbers. Faster than `Xoshiro128StarStar`, but has a [low linear
//! complexity] in the lowest bits (which are discarded when generating
//! floats), making it fail linearity tests. This is unlikely to have any
//! impact in practise.
//! - [`Xoroshiro64StarStar`]: An alternative to `Xoshiro128StarStar`, having
//! the same speed but using half the state.
//! - [`Xoroshiro64Star`]: An alternative to `Xoshiro128Plus`, having the
//! same speed but using half the state. Has a [low linear complexity] in the
//! lowest bits (which are discarded when generating floats), making it fail
//! linearity tests. This is unlikely to have any impact in practise.
//!
//! [xoshiro]: http://xoshiro.di.unimi.it/
//! [low linear complexity]: http://xoshiro.di.unimi.it/lowcomp.php
//! [`Xoshiro256StarStar`]: ./struct.Xoshiro256StarStar.html
//! [`Xoshiro256Plus`]: ./struct.Xoshiro256Plus.html
//! [`Xoroshiro128StarStar`]: ./struct.Xoroshiro128StarStar.html
//! [`Xoroshiro128Plus`]: ./struct.Xoroshiro128Plus.html
//! [`Xoshiro512StarStar`]: ./struct.Xoshiro512StarStar.html
//! [`Xoshiro512Plus`]: ./struct.Xoshiro512Plus.html
//! [`SplitMix64`]: ./struct.SplitMix64.html
//! [`Xoshiro128StarStar`]: ./struct.Xoshiro128StarStar.html
//! [`Xoshiro128Plus`]: ./struct.Xoshiro128Plus.html
//! [`Xoroshiro64StarStar`]: ./struct.Xoroshiro64StarStar.html
//! [`Xoroshiro64Star`]: ./struct.Xoroshiro64Star.html
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_xoshiro/0.1.0")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
extern crate byteorder;
extern crate rand_core;
#[macro_use]
mod common;
mod splitmix64;
mod xoshiro128starstar;
mod xoshiro128plus;
mod xoshiro256starstar;
mod xoshiro256plus;
mod xoshiro512starstar;
mod xoshiro512plus;
mod xoroshiro128plus;
mod xoroshiro128starstar;
mod xoroshiro64starstar;
mod xoroshiro64star;
pub use splitmix64::SplitMix64;
pub use xoshiro128starstar::Xoshiro128StarStar;
pub use xoshiro128plus::Xoshiro128Plus;
pub use xoshiro256starstar::Xoshiro256StarStar;
pub use xoshiro256plus::Xoshiro256Plus;
pub use common::Seed512;
pub use xoshiro512starstar::Xoshiro512StarStar;
pub use xoshiro512plus::Xoshiro512Plus;
pub use xoroshiro128plus::Xoroshiro128Plus;
pub use xoroshiro128starstar::Xoroshiro128StarStar;
pub use xoroshiro64starstar::Xoroshiro64StarStar;
pub use xoroshiro64star::Xoroshiro64Star;
+142
View File
@@ -0,0 +1,142 @@
use byteorder::{ByteOrder, LittleEndian};
use rand_core::le::read_u64_into;
use rand_core::impls::fill_bytes_via_next;
use rand_core::{RngCore, SeedableRng, Error};
/// A splitmix64 random number generator.
///
/// The splitmix algorithm is not suitable for cryptographic purposes, but is
/// very fast and has a 64 bit state.
///
/// The algorithm used here is translated from [the `splitmix64.c`
/// reference source code](http://xoshiro.di.unimi.it/splitmix64.c) by
/// Sebastiano Vigna. For `next_u32`, a more efficient mixing function taken
/// from [`dsiutils`](http://dsiutils.di.unimi.it/) is used.
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone)]
pub struct SplitMix64 {
x: u64,
}
const PHI: u64 = 0x9e3779b97f4a7c15;
impl RngCore for SplitMix64 {
#[inline]
fn next_u32(&mut self) -> u32 {
self.x = self.x.wrapping_add(PHI);
let mut z = self.x;
// David Stafford's
// (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
// "Mix4" variant of the 64-bit finalizer in Austin Appleby's
// MurmurHash3 algorithm.
z = (z ^ (z >> 33)).wrapping_mul(0x62A9D9ED799705F5);
z = (z ^ (z >> 28)).wrapping_mul(0xCB24D0A5C88C35B3);
(z >> 32) as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
self.x = self.x.wrapping_add(PHI);
let mut z = self.x;
z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
z ^ (z >> 31)
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl SeedableRng for SplitMix64 {
type Seed = [u8; 8];
/// Create a new `SplitMix64`.
fn from_seed(seed: [u8; 8]) -> SplitMix64 {
let mut state = [0; 1];
read_u64_into(&seed, &mut state);
SplitMix64 {
x: state[0],
}
}
/// Seed a `SplitMix64` from a `u64`.
fn seed_from_u64(seed: u64) -> SplitMix64 {
let mut x = [0; 8];
LittleEndian::write_u64(&mut x, seed);
SplitMix64::from_seed(x)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = SplitMix64::seed_from_u64(1477776061723855037);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/splitmix64.c
let expected = vec![
1985237415132408290, 2979275885539914483, 13511426838097143398,
8488337342461049707, 15141737807933549159, 17093170987380407015,
16389528042912955399, 13177319091862933652, 10841969400225389492,
17094824097954834098, 3336622647361835228, 9678412372263018368,
11111587619974030187, 7882215801036322410, 5709234165213761869,
7799681907651786826, 4616320717312661886, 4251077652075509767,
7836757050122171900, 5054003328188417616, 12919285918354108358,
16477564761813870717, 5124667218451240549, 18099554314556827626,
7603784838804469118, 6358551455431362471, 3037176434532249502,
3217550417701719149, 9958699920490216947, 5965803675992506258,
12000828378049868312, 12720568162811471118, 245696019213873792,
8351371993958923852, 14378754021282935786, 5655432093647472106,
5508031680350692005, 8515198786865082103, 6287793597487164412,
14963046237722101617, 3630795823534910476, 8422285279403485710,
10554287778700714153, 10871906555720704584, 8659066966120258468,
9420238805069527062, 10338115333623340156, 13514802760105037173,
14635952304031724449, 15419692541594102413,
];
for &e in &expected {
assert_eq!(rng.next_u64(), e);
}
}
#[test]
fn next_u32() {
let mut rng = SplitMix64::seed_from_u64(10);
// These values were produced with the reference implementation:
// http://dsiutils.di.unimi.it/dsiutils-2.5.1-src.tar.gz
let expected = vec![
3930361779, 4016923089, 4113052479, 925926767, 1755287528,
802865554, 954171070, 3724185978, 173676273, 1414488795, 12664133,
1784889697, 1303817078, 261610523, 941280008, 2571813643,
2954453492, 378291111, 2546873158, 3923319175, 645257028,
3881821278, 2681538690, 3037029984, 1999958137, 1853970361,
2989951788, 2126166628, 839962987, 3989679659, 3656977858,
684284364, 1673258011, 170979192, 3037622326, 1600748179,
1780764218, 1141430714, 4139736875, 3336905707, 2262051600,
3830850262, 2430765325, 1073032139, 1668888979, 2716938970,
4102420032, 40305196, 386350562, 2754480591, 622869439, 2129598760,
2306038241, 4218338739, 412298926, 3453855056, 3061469690,
4284292697, 994843708, 1591016681, 414726151, 1238182607, 18073498,
1237631493, 351884714, 2347486264, 2488990876, 802846256, 645670443,
957607012, 3126589776, 1966356370, 3036485766, 868696717,
2808613630, 2070968151, 1025536863, 1743949425, 466212687,
2994327271, 209776458, 1246125124, 3344380309, 2203947859,
968313105, 2805485302, 197484837, 3472483632, 3931823935,
3288490351, 4165666529, 3671080416, 689542830, 1272555356,
1039141475, 3984640460, 4142959054, 2252788890, 2459379590,
991872507,
];
for &e in &expected {
assert_eq!(rng.next_u32(), e);
}
}
}
+124
View File
@@ -0,0 +1,124 @@
use rand_core;
use rand_core::le::read_u64_into;
use rand_core::impls::fill_bytes_via_next;
use rand_core::{RngCore, SeedableRng};
/// A xoroshiro128+ random number generator.
///
/// The xoroshiro128+ algorithm is not suitable for cryptographic purposes, but
/// is very fast and has good statistical properties, besides a low linear
/// complexity in the lowest bits.
///
/// The algorithm used here is translated from [the `xoroshiro128plus.c`
/// reference source code](http://xoshiro.di.unimi.it/xoroshiro128plus.c) by
/// David Blackman and Sebastiano Vigna.
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone)]
pub struct Xoroshiro128Plus {
s0: u64,
s1: u64,
}
impl Xoroshiro128Plus {
/// Jump forward, equivalently to 2^64 calls to `next_u64()`.
///
/// This can be used to generate 2^64 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoroshiro128Plus;
///
/// let rng1 = Xoroshiro128Plus::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]);
}
/// Jump forward, equivalently to 2^96 calls to `next_u64()`.
///
/// This can be used to generate 2^32 starting points, from each of which
/// `jump()` will generate 2^32 non-overlapping subsequences for parallel
/// distributed computations.
pub fn long_jump(&mut self) {
impl_jump!(u64, self, [0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1]);
}
}
impl RngCore for Xoroshiro128Plus {
#[inline]
fn next_u32(&mut self) -> u32 {
// The two lowest bits have some linear dependencies, so we use the
// upper bits instead.
(self.next_u64() >> 32) as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
let r = self.s0.wrapping_add(self.s1);
impl_xoroshiro_u64!(self);
r
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl SeedableRng for Xoroshiro128Plus {
type Seed = [u8; 16];
/// Create a new `Xoroshiro128Plus`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
fn from_seed(seed: [u8; 16]) -> Xoroshiro128Plus {
deal_with_zero_seed!(seed, Self);
let mut s = [0; 2];
read_u64_into(&seed, &mut s);
Xoroshiro128Plus {
s0: s[0],
s1: s[1],
}
}
/// Seed a `Xoroshiro128Plus` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoroshiro128Plus {
from_splitmix!(seed)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoroshiro128Plus::from_seed(
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro128starstar.c
let expected = [
3, 412333834243, 2360170716294286339, 9295852285959843169,
2797080929874688578, 6019711933173041966, 3076529664176959358,
3521761819100106140, 7493067640054542992, 920801338098114767,
];
for &e in &expected {
assert_eq!(rng.next_u64(), e);
}
}
}
+121
View File
@@ -0,0 +1,121 @@
use rand_core;
use rand_core::le::read_u64_into;
use rand_core::impls::fill_bytes_via_next;
use rand_core::{RngCore, SeedableRng};
/// A xoroshiro128** random number generator.
///
/// The xoroshiro128** algorithm is not suitable for cryptographic purposes, but
/// is very fast and has excellent statistical properties.
///
/// The algorithm used here is translated from [the `xoroshiro128starstar.c`
/// reference source code](http://xoshiro.di.unimi.it/xoroshiro128starstar.c) by
/// David Blackman and Sebastiano Vigna.
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone)]
pub struct Xoroshiro128StarStar {
s0: u64,
s1: u64,
}
impl Xoroshiro128StarStar {
/// Jump forward, equivalently to 2^64 calls to `next_u64()`.
///
/// This can be used to generate 2^64 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoroshiro128StarStar;
///
/// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]);
}
/// Jump forward, equivalently to 2^96 calls to `next_u64()`.
///
/// This can be used to generate 2^32 starting points, from each of which
/// `jump()` will generate 2^32 non-overlapping subsequences for parallel
/// distributed computations.
pub fn long_jump(&mut self) {
impl_jump!(u64, self, [0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1]);
}
}
impl RngCore for Xoroshiro128StarStar {
#[inline]
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
let r = starstar_u64!(self.s0);
impl_xoroshiro_u64!(self);
r
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl SeedableRng for Xoroshiro128StarStar {
type Seed = [u8; 16];
/// Create a new `Xoroshiro128StarStar`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
fn from_seed(seed: [u8; 16]) -> Xoroshiro128StarStar {
deal_with_zero_seed!(seed, Self);
let mut s = [0; 2];
read_u64_into(&seed, &mut s);
Xoroshiro128StarStar {
s0: s[0],
s1: s[1],
}
}
/// Seed a `Xoroshiro128StarStar` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoroshiro128StarStar {
from_splitmix!(seed)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoroshiro128StarStar::from_seed(
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro128starstar.c
let expected = [
5760, 97769243520, 9706862127477703552, 9223447511460779954,
8358291023205304566, 15695619998649302768, 8517900938696309774,
16586480348202605369, 6959129367028440372, 16822147227405758281,
];
for &e in &expected {
assert_eq!(rng.next_u64(), e);
}
}
}
+89
View File
@@ -0,0 +1,89 @@
use byteorder::{ByteOrder, LittleEndian};
use rand_core;
use rand_core::le::read_u32_into;
use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};
use rand_core::{RngCore, SeedableRng};
/// A xoroshiro64* random number generator.
///
/// The xoroshiro64* algorithm is not suitable for cryptographic purposes, but
/// is very fast and has good statistical properties, besides a low linear
/// complexity in the lowest bits.
///
/// The algorithm used here is translated from [the `xoroshiro64star.c`
/// reference source code](http://xoshiro.di.unimi.it/xoroshiro64star.c) by
/// David Blackman and Sebastiano Vigna.
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone)]
pub struct Xoroshiro64Star {
s0: u32,
s1: u32,
}
impl RngCore for Xoroshiro64Star {
#[inline]
fn next_u32(&mut self) -> u32 {
let r = self.s0.wrapping_mul(0x9E3779BB);
impl_xoroshiro_u32!(self);
r
}
#[inline]
fn next_u64(&mut self) -> u64 {
next_u64_via_u32(self)
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl SeedableRng for Xoroshiro64Star {
type Seed = [u8; 8];
/// Create a new `Xoroshiro64Star`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
fn from_seed(seed: [u8; 8]) -> Xoroshiro64Star {
deal_with_zero_seed!(seed, Self);
let mut s = [0; 2];
read_u32_into(&seed, &mut s);
Xoroshiro64Star {
s0: s[0],
s1: s[1],
}
}
/// Seed a `Xoroshiro64Star` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoroshiro64Star {
let mut s = [0; 8];
LittleEndian::write_u64(&mut s, seed);
Xoroshiro64Star::from_seed(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoroshiro64Star::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro64star.c
let expected = [
2654435771, 327208753, 4063491769, 4259754937, 261922412, 168123673,
552743735, 1672597395, 1031040050, 2755315674,
];
for &e in &expected {
assert_eq!(rng.next_u32(), e);
}
}
}
+88
View File
@@ -0,0 +1,88 @@
use byteorder::{ByteOrder, LittleEndian};
use rand_core;
use rand_core::le::read_u32_into;
use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};
use rand_core::{RngCore, SeedableRng};
/// A Xoroshiro64** random number generator.
///
/// The xoshiro64** algorithm is not suitable for cryptographic purposes, but
/// is very fast and has excellent statistical properties.
///
/// The algorithm used here is translated from [the `xoroshiro64starstar.c`
/// reference source code](http://xoshiro.di.unimi.it/xoroshiro64starstar.c) by
/// David Blackman and Sebastiano Vigna.
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone)]
pub struct Xoroshiro64StarStar {
s0: u32,
s1: u32,
}
impl RngCore for Xoroshiro64StarStar {
#[inline]
fn next_u32(&mut self) -> u32 {
let r = starstar_u32!(self.s0);
impl_xoroshiro_u32!(self);
r
}
#[inline]
fn next_u64(&mut self) -> u64 {
next_u64_via_u32(self)
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl SeedableRng for Xoroshiro64StarStar {
type Seed = [u8; 8];
/// Create a new `Xoroshiro64StarStar`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
fn from_seed(seed: [u8; 8]) -> Xoroshiro64StarStar {
deal_with_zero_seed!(seed, Self);
let mut s = [0; 2];
read_u32_into(&seed, &mut s);
Xoroshiro64StarStar {
s0: s[0],
s1: s[1],
}
}
/// Seed a `Xoroshiro64StarStar` from a `u64`.
fn seed_from_u64(seed: u64) -> Xoroshiro64StarStar {
let mut s = [0; 8];
LittleEndian::write_u64(&mut s, seed);
Xoroshiro64StarStar::from_seed(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoroshiro64StarStar::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro64starstar.c
let expected = [
3802928447, 813792938, 1618621494, 2955957307, 3252880261,
1129983909, 2539651700, 1327610908, 1757650787, 2763843748,
];
for &e in &expected {
assert_eq!(rng.next_u32(), e);
}
}
}
+106
View File
@@ -0,0 +1,106 @@
use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next};
use rand_core::le::read_u32_into;
use rand_core::{SeedableRng, RngCore, Error};
/// A xoshiro128+ random number generator.
///
/// The xoshiro128+ algorithm is not suitable for cryptographic purposes, but
/// is very fast and has good statistical properties, besides a low linear
/// complexity in the lowest bits.
///
/// The algorithm used here is translated from [the `xoshiro128starstar.c`
/// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by
/// David Blackman and Sebastiano Vigna.
#[derive(Debug, Clone)]
pub struct Xoshiro128Plus {
s: [u32; 4],
}
impl Xoshiro128Plus {
/// Jump forward, equivalently to 2^64 calls to `next_u32()`.
///
/// This can be used to generate 2^64 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoroshiro128StarStar;
///
/// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
}
}
impl SeedableRng for Xoshiro128Plus {
type Seed = [u8; 16];
/// Create a new `Xoshiro128Plus`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
#[inline]
fn from_seed(seed: [u8; 16]) -> Xoshiro128Plus {
deal_with_zero_seed!(seed, Self);
let mut state = [0; 4];
read_u32_into(&seed, &mut state);
Xoshiro128Plus { s: state }
}
/// Seed a `Xoshiro128Plus` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoshiro128Plus {
from_splitmix!(seed)
}
}
impl RngCore for Xoshiro128Plus {
#[inline]
fn next_u32(&mut self) -> u32 {
let result_plus = self.s[0].wrapping_add(self.s[3]);
impl_xoshiro_u32!(self);
result_plus
}
#[inline]
fn next_u64(&mut self) -> u64 {
next_u64_via_u32(self)
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoshiro128Plus::from_seed(
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro128plus.c
let expected = [
5, 12295, 25178119, 27286542, 39879690, 1140358681, 3276312097,
4110231701, 399823256, 2144435200,
];
for &e in &expected {
assert_eq!(rng.next_u32(), e);
}
}
}
+105
View File
@@ -0,0 +1,105 @@
use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next};
use rand_core::le::read_u32_into;
use rand_core::{SeedableRng, RngCore, Error};
/// A xoshiro128** random number generator.
///
/// The xoshiro128** algorithm is not suitable for cryptographic purposes, but
/// is very fast and has excellent statistical properties.
///
/// The algorithm used here is translated from [the `xoshiro128starstar.c`
/// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by
/// David Blackman and Sebastiano Vigna.
#[derive(Debug, Clone)]
pub struct Xoshiro128StarStar {
s: [u32; 4],
}
impl Xoshiro128StarStar {
/// Jump forward, equivalently to 2^64 calls to `next_u32()`.
///
/// This can be used to generate 2^64 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoroshiro128StarStar;
///
/// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
}
}
impl SeedableRng for Xoshiro128StarStar {
type Seed = [u8; 16];
/// Create a new `Xoshiro128StarStar`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
#[inline]
fn from_seed(seed: [u8; 16]) -> Xoshiro128StarStar {
deal_with_zero_seed!(seed, Self);
let mut state = [0; 4];
read_u32_into(&seed, &mut state);
Xoshiro128StarStar { s: state }
}
/// Seed a `Xoshiro128StarStar` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoshiro128StarStar {
from_splitmix!(seed)
}
}
impl RngCore for Xoshiro128StarStar {
#[inline]
fn next_u32(&mut self) -> u32 {
let result_starstar = starstar_u64!(self.s[0]);
impl_xoshiro_u32!(self);
result_starstar
}
#[inline]
fn next_u64(&mut self) -> u64 {
next_u64_via_u32(self)
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoshiro128StarStar::from_seed(
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro128starstar.c
let expected = [
5760, 40320, 70819200, 3297914139, 2480851620, 1792823698,
4118739149, 1251203317, 1581886583, 1721184582,
];
for &e in &expected {
assert_eq!(rng.next_u32(), e);
}
}
}
+125
View File
@@ -0,0 +1,125 @@
use rand_core::impls::fill_bytes_via_next;
use rand_core::le::read_u64_into;
use rand_core::{SeedableRng, RngCore, Error};
/// A xoshiro256+ random number generator.
///
/// The xoshiro256+ algorithm is not suitable for cryptographic purposes, but
/// is very fast and has good statistical properties, besides a low linear
/// complexity in the lowest bits.
///
/// The algorithm used here is translated from [the `xoshiro256plus.c`
/// reference source code](http://xoshiro.di.unimi.it/xoshiro256plus.c) by
/// David Blackman and Sebastiano Vigna.
#[derive(Debug, Clone)]
pub struct Xoshiro256Plus {
s: [u64; 4],
}
impl Xoshiro256Plus {
/// Jump forward, equivalently to 2^128 calls to `next_u64()`.
///
/// This can be used to generate 2^128 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoshiro256Plus;
///
/// let rng1 = Xoshiro256Plus::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u64, self, [
0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
0xa9582618e03fc9aa, 0x39abdc4529b1661c
]);
}
/// Jump forward, equivalently to 2^192 calls to `next_u64()`.
///
/// This can be used to generate 2^64 starting points, from each of which
/// `jump()` will generate 2^64 non-overlapping subsequences for parallel
/// distributed computations.
pub fn long_jump(&mut self) {
impl_jump!(u64, self, [
0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
0x77710069854ee241, 0x39109bb02acbe635
]);
}
}
impl SeedableRng for Xoshiro256Plus {
type Seed = [u8; 32];
/// Create a new `Xoshiro256Plus`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
#[inline]
fn from_seed(seed: [u8; 32]) -> Xoshiro256Plus {
deal_with_zero_seed!(seed, Self);
let mut state = [0; 4];
read_u64_into(&seed, &mut state);
Xoshiro256Plus { s: state }
}
/// Seed a `Xoshiro256Plus` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoshiro256Plus {
from_splitmix!(seed)
}
}
impl RngCore for Xoshiro256Plus {
#[inline]
fn next_u32(&mut self) -> u32 {
// The lowest bits have some linear dependencies, so we use the
// upper bits instead.
(self.next_u64() >> 32) as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
let result_plus = self.s[0].wrapping_add(self.s[3]);
impl_xoshiro_u64!(self);
result_plus
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoshiro256Plus::from_seed(
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro256plus.c
let expected = [
5, 211106232532999, 211106635186183, 9223759065350669058,
9250833439874351877, 13862484359527728515, 2346507365006083650,
1168864526675804870, 34095955243042024, 3466914240207415127,
];
for &e in &expected {
assert_eq!(rng.next_u64(), e);
}
}
}
+122
View File
@@ -0,0 +1,122 @@
use rand_core::impls::fill_bytes_via_next;
use rand_core::le::read_u64_into;
use rand_core::{SeedableRng, RngCore, Error};
/// A xoshiro256** random number generator.
///
/// The xoshiro256** algorithm is not suitable for cryptographic purposes, but
/// is very fast and has excellent statistical properties.
///
/// The algorithm used here is translated from [the `xoshiro256starstar.c`
/// reference source code](http://xoshiro.di.unimi.it/xoshiro256starstar.c) by
/// David Blackman and Sebastiano Vigna.
#[derive(Debug, Clone)]
pub struct Xoshiro256StarStar {
s: [u64; 4],
}
impl Xoshiro256StarStar {
/// Jump forward, equivalently to 2^128 calls to `next_u64()`.
///
/// This can be used to generate 2^128 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoshiro256StarStar;
///
/// let rng1 = Xoshiro256StarStar::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u64, self, [
0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
0xa9582618e03fc9aa, 0x39abdc4529b1661c
]);
}
/// Jump forward, equivalently to 2^192 calls to `next_u64()`.
///
/// This can be used to generate 2^64 starting points, from each of which
/// `jump()` will generate 2^64 non-overlapping subsequences for parallel
/// distributed computations.
pub fn long_jump(&mut self) {
impl_jump!(u64, self, [
0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
0x77710069854ee241, 0x39109bb02acbe635
]);
}
}
impl SeedableRng for Xoshiro256StarStar {
type Seed = [u8; 32];
/// Create a new `Xoshiro256StarStar`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
#[inline]
fn from_seed(seed: [u8; 32]) -> Xoshiro256StarStar {
deal_with_zero_seed!(seed, Self);
let mut state = [0; 4];
read_u64_into(&seed, &mut state);
Xoshiro256StarStar { s: state }
}
/// Seed a `Xoshiro256StarStar` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoshiro256StarStar {
from_splitmix!(seed)
}
}
impl RngCore for Xoshiro256StarStar {
#[inline]
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
let result_starstar = starstar_u64!(self.s[1]);
impl_xoshiro_u64!(self);
result_starstar
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoshiro256StarStar::from_seed(
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro128starstar.c
let expected = [
11520, 0, 1509978240, 1215971899390074240, 1216172134540287360,
607988272756665600, 16172922978634559625, 8476171486693032832,
10595114339597558777, 2904607092377533576,
];
for &e in &expected {
assert_eq!(rng.next_u64(), e);
}
}
}
+116
View File
@@ -0,0 +1,116 @@
use rand_core::impls::fill_bytes_via_next;
use rand_core::le::read_u64_into;
use rand_core::{SeedableRng, RngCore, Error};
use Seed512;
/// A xoshiro512+ random number generator.
///
/// The xoshiro512+ algorithm is not suitable for cryptographic purposes, but
/// is very fast and has good statistical properties, besides a low linear
/// complexity in the lowest bits.
///
/// The algorithm used here is translated from [the `xoshiro512plus.c`
/// reference source code](http://xoshiro.di.unimi.it/xoshiro512plus.c) by
/// David Blackman and Sebastiano Vigna.
#[derive(Debug, Clone)]
pub struct Xoshiro512Plus {
s: [u64; 8],
}
impl Xoshiro512Plus {
/// Jump forward, equivalently to 2^256 calls to `next_u64()`.
///
/// This can be used to generate 2^256 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoshiro512Plus;
///
/// let rng1 = Xoshiro512Plus::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u64, self, [
0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae,
0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c,
0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db
]);
}
}
impl SeedableRng for Xoshiro512Plus {
type Seed = Seed512;
/// Create a new `Xoshiro512Plus`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
#[inline]
fn from_seed(seed: Seed512) -> Xoshiro512Plus {
deal_with_zero_seed!(seed, Self);
let mut state = [0; 8];
read_u64_into(&seed.0, &mut state);
Xoshiro512Plus { s: state }
}
/// Seed a `Xoshiro512Plus` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoshiro512Plus {
from_splitmix!(seed)
}
}
impl RngCore for Xoshiro512Plus {
#[inline]
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
let result_plus = self.s[0].wrapping_add(self.s[2]);
impl_xoshiro_large!(self);
result_plus
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoshiro512Plus::from_seed(Seed512(
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0,
7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0]));
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro512plus.c
let expected = [
4, 8, 4113, 25169936, 52776585412635, 57174648719367,
9223482039571869716, 9331471677901559830, 9340533895746033672,
14078399799840753678,
];
for &e in &expected {
assert_eq!(rng.next_u64(), e);
}
}
}
+116
View File
@@ -0,0 +1,116 @@
use rand_core::impls::fill_bytes_via_next;
use rand_core::le::read_u64_into;
use rand_core::{SeedableRng, RngCore, Error};
use Seed512;
/// A xoshiro512** random number generator.
///
/// The xoshiro512** algorithm is not suitable for cryptographic purposes, but
/// is very fast and has excellent statistical properties.
///
/// The algorithm used here is translated from [the `xoshiro512starstar.c`
/// reference source code](http://xoshiro.di.unimi.it/xoshiro512starstar.c) by
/// David Blackman and Sebastiano Vigna.
#[derive(Debug, Clone)]
pub struct Xoshiro512StarStar {
s: [u64; 8],
}
impl Xoshiro512StarStar {
/// Jump forward, equivalently to 2^256 calls to `next_u64()`.
///
/// This can be used to generate 2^256 non-overlapping subsequences for
/// parallel computations.
///
/// ```
/// # extern crate rand;
/// # extern crate rand_xoshiro;
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xoshiro::Xoshiro512StarStar;
///
/// let rng1 = Xoshiro512StarStar::seed_from_u64(0);
/// let mut rng2 = rng1.clone();
/// rng2.jump();
/// let mut rng3 = rng2.clone();
/// rng3.jump();
/// # }
/// ```
pub fn jump(&mut self) {
impl_jump!(u64, self, [
0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae,
0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c,
0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db
]);
}
}
impl SeedableRng for Xoshiro512StarStar {
type Seed = Seed512;
/// Create a new `Xoshiro512StarStar`. If `seed` is entirely 0, it will be
/// mapped to a different seed.
#[inline]
fn from_seed(seed: Seed512) -> Xoshiro512StarStar {
deal_with_zero_seed!(seed, Self);
let mut state = [0; 8];
read_u64_into(&seed.0, &mut state);
Xoshiro512StarStar { s: state }
}
/// Seed a `Xoshiro512StarStar` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoshiro512StarStar {
from_splitmix!(seed)
}
}
impl RngCore for Xoshiro512StarStar {
#[inline]
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
let result_starstar = starstar_u64!(self.s[1]);
impl_xoshiro_large!(self);
result_starstar
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference() {
let mut rng = Xoshiro512StarStar::from_seed(Seed512(
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0,
7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0]));
// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro512starstar.c
let expected = [
11520, 0, 23040, 23667840, 144955163520, 303992986974289920,
25332796375735680, 296904390158016, 13911081092387501979,
15304787717237593024,
];
for &e in &expected {
assert_eq!(rng.next_u64(), e);
}
}
}
+2 -2
View File
@@ -151,12 +151,12 @@ impl SeedableRng for ChaChaRng {
}
impl ChaChaRng {
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
pub fn get_word_pos(&self) -> u128 {
self.0.get_word_pos()
}
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
pub fn set_word_pos(&mut self, word_offset: u128) {
self.0.set_word_pos(word_offset)
}
+4 -4
View File
@@ -45,7 +45,7 @@ impl Distribution<u64> for Standard {
}
}
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
impl Distribution<u128> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u128 {
@@ -85,7 +85,7 @@ impl_int_from_uint! { i8, u8 }
impl_int_from_uint! { i16, u16 }
impl_int_from_uint! { i32, u32 }
impl_int_from_uint! { i64, u64 }
#[cfg(rust_1_26)] impl_int_from_uint! { i128, u128 }
#[cfg(rustc_1_26)] impl_int_from_uint! { i128, u128 }
impl_int_from_uint! { isize, usize }
#[cfg(feature="simd_support")]
@@ -147,7 +147,7 @@ mod tests {
rng.sample::<i16, _>(Standard);
rng.sample::<i32, _>(Standard);
rng.sample::<i64, _>(Standard);
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
rng.sample::<i128, _>(Standard);
rng.sample::<usize, _>(Standard);
@@ -155,7 +155,7 @@ mod tests {
rng.sample::<u16, _>(Standard);
rng.sample::<u32, _>(Standard);
rng.sample::<u64, _>(Standard);
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
rng.sample::<u128, _>(Standard);
}
}
+3 -3
View File
@@ -182,7 +182,7 @@
//! [`Weibull`]: struct.Weibull.html
//! [`WeightedIndex`]: struct.WeightedIndex.html
#[cfg(any(rust_1_26, features="nightly"))]
#[cfg(any(rustc_1_26, features="nightly"))]
use core::iter;
use Rng;
@@ -316,7 +316,7 @@ impl<'a, D, R, T> Iterator for DistIter<'a, D, R, T>
}
}
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
impl<'a, D, R, T> iter::FusedIterator for DistIter<'a, D, R, T>
where D: Distribution<T>, R: Rng + 'a {}
@@ -328,7 +328,7 @@ impl<'a, D, R, T> iter::TrustedLen for DistIter<'a, D, R, T>
/// A generic random value distribution, implemented for many primitive types.
/// Usually generates values with a numerically uniform distribution, and with a
/// range appropriate to the type.
///
///
/// ## Built-in Implementations
///
/// Assuming the provided `Rng` is well-behaved, these implementations
+19 -13
View File
@@ -111,6 +111,8 @@
#[cfg(feature = "std")]
use std::time::Duration;
#[cfg(all(not(feature = "std"), rustc_1_25))]
use core::time::Duration;
use Rng;
use distributions::Distribution;
@@ -275,7 +277,7 @@ impl<X: SampleUniform> From<::core::ops::Range<X>> for Uniform<X> {
}
}
#[cfg(rust_1_27)]
#[cfg(rustc_1_27)]
impl<X: SampleUniform> From<::core::ops::RangeInclusive<X>> for Uniform<X> {
fn from(r: ::core::ops::RangeInclusive<X>) -> Uniform<X> {
Uniform::new_inclusive(r.start(), r.end())
@@ -450,8 +452,9 @@ macro_rules! uniform_int_impl {
let ints_to_reject = (unsigned_max - range + 1) % range;
unsigned_max - ints_to_reject
} else {
// conservative but fast approximation
range << range.leading_zeros()
// conservative but fast approximation. `- 1` is necessary to allow the
// same comparison without bias.
(range << range.leading_zeros()).wrapping_sub(1)
};
loop {
@@ -470,7 +473,7 @@ uniform_int_impl! { i8, i8, u8, i32, u32 }
uniform_int_impl! { i16, i16, u16, i32, u32 }
uniform_int_impl! { i32, i32, u32, i32, u32 }
uniform_int_impl! { i64, i64, u64, i64, u64 }
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
uniform_int_impl! { i128, i128, u128, u128, u128 }
uniform_int_impl! { isize, isize, usize, isize, usize }
uniform_int_impl! { u8, i8, u8, i32, u32 }
@@ -478,7 +481,7 @@ uniform_int_impl! { u16, i16, u16, i32, u32 }
uniform_int_impl! { u32, i32, u32, i32, u32 }
uniform_int_impl! { u64, i64, u64, i64, u64 }
uniform_int_impl! { usize, isize, usize, isize, usize }
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
uniform_int_impl! { u128, u128, u128, i128, u128 }
#[cfg(all(feature = "simd_support", feature = "nightly"))]
@@ -833,14 +836,14 @@ uniform_float_impl! { f64x8, u64x8, f64, u64, 64 - 52 }
///
/// [`UniformSampler`]: trait.UniformSampler.html
/// [`Uniform`]: struct.Uniform.html
#[cfg(feature = "std")]
#[cfg(any(feature = "std", rustc_1_25))]
#[derive(Clone, Copy, Debug)]
pub struct UniformDuration {
mode: UniformDurationMode,
offset: u32,
}
#[cfg(feature = "std")]
#[cfg(any(feature = "std", rustc_1_25))]
#[derive(Debug, Copy, Clone)]
enum UniformDurationMode {
Small {
@@ -857,12 +860,12 @@ enum UniformDurationMode {
}
}
#[cfg(feature = "std")]
#[cfg(any(feature = "std", rustc_1_25))]
impl SampleUniform for Duration {
type Sampler = UniformDuration;
}
#[cfg(feature = "std")]
#[cfg(any(feature = "std", rustc_1_25))]
impl UniformSampler for UniformDuration {
type X = Duration;
@@ -987,7 +990,7 @@ mod tests {
fn test_integers() {
use core::{i8, i16, i32, i64, isize};
use core::{u8, u16, u32, u64, usize};
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
use core::{i128, u128};
let mut rng = ::test::rng(251);
@@ -1051,7 +1054,7 @@ mod tests {
}
t!(i8, i16, i32, i64, isize,
u8, u16, u32, u64, usize);
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
t!(i128, u128);
#[cfg(all(feature = "simd_support", feature = "nightly"))]
@@ -1206,9 +1209,12 @@ mod tests {
#[test]
#[cfg(feature = "std")]
#[cfg(any(feature = "std", rustc_1_25))]
fn test_durations() {
#[cfg(feature = "std")]
use std::time::Duration;
#[cfg(all(not(feature = "std"), rustc_1_25))]
use core::time::Duration;
let mut rng = ::test::rng(253);
@@ -1278,7 +1284,7 @@ mod tests {
assert_eq!(r.inner.scale, 5.0);
}
#[cfg(rust_1_27)]
#[cfg(rustc_1_27)]
#[test]
fn test_uniform_from_std_range_inclusive() {
let r = Uniform::from(2u32..=6);
+5 -6
View File
@@ -29,27 +29,26 @@ use distributions::{Distribution, Uniform};
/// NBS Appl. Math. Ser., No. 12. Washington, DC: U.S. Government Printing
/// Office, pp. 36-38.
#[derive(Clone, Copy, Debug)]
pub struct UnitCircle {
uniform: Uniform<f64>,
}
pub struct UnitCircle;
impl UnitCircle {
/// Construct a new `UnitCircle` distribution.
#[inline]
pub fn new() -> UnitCircle {
UnitCircle { uniform: Uniform::new(-1., 1.) }
UnitCircle
}
}
impl Distribution<[f64; 2]> for UnitCircle {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [f64; 2] {
let uniform = Uniform::new(-1., 1.);
let mut x1;
let mut x2;
let mut sum;
loop {
x1 = self.uniform.sample(rng);
x2 = self.uniform.sample(rng);
x1 = uniform.sample(rng);
x2 = uniform.sample(rng);
sum = x1*x1 + x2*x2;
if sum < 1. {
break;
+4 -5
View File
@@ -28,23 +28,22 @@ use distributions::{Distribution, Uniform};
/// Sphere.*](https://doi.org/10.1214/aoms/1177692644)
/// Ann. Math. Statist. 43, no. 2, 645--646.
#[derive(Clone, Copy, Debug)]
pub struct UnitSphereSurface {
uniform: Uniform<f64>,
}
pub struct UnitSphereSurface;
impl UnitSphereSurface {
/// Construct a new `UnitSphereSurface` distribution.
#[inline]
pub fn new() -> UnitSphereSurface {
UnitSphereSurface { uniform: Uniform::new(-1., 1.) }
UnitSphereSurface
}
}
impl Distribution<[f64; 3]> for UnitSphereSurface {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [f64; 3] {
let uniform = Uniform::new(-1., 1.);
loop {
let (x1, x2) = (self.uniform.sample(rng), self.uniform.sample(rng));
let (x1, x2) = (uniform.sample(rng), uniform.sample(rng));
let sum = x1*x1 + x2*x2;
if sum >= 1. {
continue;
+3 -3
View File
@@ -61,7 +61,7 @@ macro_rules! wmul_impl {
wmul_impl! { u8, u16, 8 }
wmul_impl! { u16, u32, 16 }
wmul_impl! { u32, u64, 32 }
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
wmul_impl! { u64, u128, 64 }
// This code is a translation of the __mulddi3 function in LLVM's
@@ -125,9 +125,9 @@ macro_rules! wmul_impl_large {
)+
};
}
#[cfg(not(rust_1_26))]
#[cfg(not(rustc_1_26))]
wmul_impl_large! { u64, 32 }
#[cfg(rust_1_26)]
#[cfg(rustc_1_26)]
wmul_impl_large! { u128, 64 }
macro_rules! wmul_impl_usize {
+3 -3
View File
@@ -49,7 +49,7 @@
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand/0.6.0")]
html_root_url = "https://rust-random.github.io/rand/")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
@@ -549,13 +549,13 @@ macro_rules! impl_as_byte_slice {
impl_as_byte_slice!(u16);
impl_as_byte_slice!(u32);
impl_as_byte_slice!(u64);
#[cfg(rust_1_26)] impl_as_byte_slice!(u128);
#[cfg(rustc_1_26)] impl_as_byte_slice!(u128);
impl_as_byte_slice!(usize);
impl_as_byte_slice!(i8);
impl_as_byte_slice!(i16);
impl_as_byte_slice!(i32);
impl_as_byte_slice!(i64);
#[cfg(rust_1_26)] impl_as_byte_slice!(i128);
#[cfg(rustc_1_26)] impl_as_byte_slice!(i128);
impl_as_byte_slice!(isize);
macro_rules! impl_as_byte_slice_arrays {
+4 -4
View File
@@ -10,9 +10,9 @@
use {RngCore, SeedableRng, Error};
#[cfg(all(rust_1_26, target_pointer_width = "64"))]
#[cfg(all(rustc_1_26, target_pointer_width = "64"))]
type Rng = ::rand_pcg::Pcg64Mcg;
#[cfg(not(all(rust_1_26, target_pointer_width = "64")))]
#[cfg(not(all(rustc_1_26, target_pointer_width = "64")))]
type Rng = ::rand_pcg::Pcg32;
/// An RNG recommended when small state, cheap initialization and good
@@ -67,8 +67,8 @@ type Rng = ::rand_pcg::Pcg32;
/// [`FromEntropy`]: ../trait.FromEntropy.html
/// [`StdRng`]: struct.StdRng.html
/// [`thread_rng`]: ../fn.thread_rng.html
/// [`Pcg64Mcg`]: https://docs.rs/rand_pcg/0.1.0/rand_pcg/type.Pcg64Mcg.html
/// [`Pcg32`]: https://docs.rs/rand_pcg/0.1.0/rand_pcg/type.Pcg32.html
/// [`Pcg64Mcg`]: ../../rand_pcg/type.Pcg64Mcg.html
/// [`Pcg32`]: ../../rand_pcg/type.Pcg32.html
#[derive(Clone, Debug)]
pub struct SmallRng(Rng);
+11 -4
View File
@@ -87,10 +87,11 @@ thread_local!(
}
);
/// Retrieve the lazily-initialized thread-local random number
/// generator, seeded by the system. Intended to be used in method
/// chaining style, e.g. `thread_rng().gen::<i32>()`, or cached locally, e.g.
/// `let mut rng = thread_rng();`.
/// Retrieve the lazily-initialized thread-local random number generator,
/// seeded by the system. Intended to be used in method chaining style,
/// e.g. `thread_rng().gen::<i32>()`, or cached locally, e.g.
/// `let mut rng = thread_rng();`. Invoked by the `Default` trait, making
/// `ThreadRng::default()` equivelent.
///
/// For more information see [`ThreadRng`].
///
@@ -99,6 +100,12 @@ pub fn thread_rng() -> ThreadRng {
ThreadRng { rng: THREAD_RNG_KEY.with(|t| t.get()) }
}
impl Default for ThreadRng {
fn default() -> ThreadRng {
::prelude::thread_rng()
}
}
impl RngCore for ThreadRng {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
+15
View File
@@ -0,0 +1,15 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
CARGO_WEB_RELEASE=$(curl -L -s -H 'Accept: application/json' https://github.com/koute/cargo-web/releases/latest)
CARGO_WEB_VERSION=$(echo $CARGO_WEB_RELEASE | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')
CARGO_WEB_URL="https://github.com/koute/cargo-web/releases/download/$CARGO_WEB_VERSION/cargo-web-x86_64-unknown-linux-gnu.gz"
echo "Downloading cargo-web from: $CARGO_WEB_URL"
curl -L $CARGO_WEB_URL | gzip -d > cargo-web
chmod +x cargo-web
mkdir -p ~/.cargo/bin
mv cargo-web ~/.cargo/bin
+1
View File
@@ -12,6 +12,7 @@ main() {
cross test --target $TARGET --manifest-path rand_isaac/Cargo.toml --features=serde1
cross test --target $TARGET --manifest-path rand_pcg/Cargo.toml --features=serde1
cross test --target $TARGET --manifest-path rand_xorshift/Cargo.toml --features=serde1
cross test --target $TARGET --manifest-path rand_xoshiro/Cargo.toml
cross test --target $TARGET --manifest-path rand_chacha/Cargo.toml
cross test --target $TARGET --manifest-path rand_hc/Cargo.toml
}