rand_xoshiro-0.4.0/CHANGELOG.md010066400051560005156000000017731353324246600142760ustar0000000000000000# 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.4.0] - 2019-09-03 - Add xoshiro128++, 256++ and 512++ variants - Add xoroshiro128++ variant - Add `long_jump` method to RNGs missing it - Update xoshiro128** to version 1.1, breaking value stability ## [0.3.1] - 2019-08-06 - Drop `byteorder`-dependency in favor of `stdlib`-implementation. ## [0.3.0] - 2019-06-12 - Bump minor crate version since rand_core bump is a breaking change - Switch to Edition 2018 ## [0.2.1] - 2019-06-06 - yanked - Bump `rand_core` version - Document crate features in README ## [0.2.0] - 2019-05-28 - Fix `seed_from_u64(0)` for `Xoroshiro64StarStar` and `Xoroshiro64Star`. This breaks value stability for these generators if initialized with `seed_from_u64`. - Implement Serde support. ## [0.1.0] - 2019-01-04 Initial release. rand_xoshiro-0.4.0/COPYRIGHT010066400051560005156000000010711350313257000137360ustar0000000000000000Copyrights in the Rand project are retained by their contributors. No copyright assignment is required to contribute to the Rand project. For full authorship information, see the version control history. Except as otherwise noted (below and/or in individual files), Rand is licensed under the Apache License, Version 2.0 or or the MIT license or , at your option. The Rand project includes code from the Rust project published under these same licenses. rand_xoshiro-0.4.0/Cargo.toml.orig010066400051560005156000000015271353324246600153510ustar0000000000000000[package] name = "rand_xoshiro" version = "0.4.0" # NB: When modifying, also modify html_root_url in lib.rs authors = ["The Rand Project Developers"] license = "MIT OR 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"] edition = "2018" [features] serde1 = ["serde"] [dependencies] rand_core = { path = "../rand_core", version = "0.5" } serde = { version = "1", features = ["derive"], optional=true } [dev-dependencies] # This is for testing serde, unfortunately we can't specify feature-gated dev # deps yet, see: https://github.com/rust-lang/cargo/issues/1596 bincode = { version = "1" } rand_xoshiro-0.4.0/Cargo.toml0000644000000022010000000000000115640ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "rand_xoshiro" version = "0.4.0" authors = ["The Rand Project Developers"] description = "Xoshiro, xoroshiro and splitmix64 random number generators" homepage = "https://crates.io/crates/rand_xoshiro" documentation = "https://docs.rs/rand_xoshiro" readme = "README.md" keywords = ["random", "rng"] categories = ["algorithms"] license = "MIT OR Apache-2.0" repository = "https://github.com/rust-random/rand" [dependencies.rand_core] version = "0.5" [dependencies.serde] version = "1" features = ["derive"] optional = true [dev-dependencies.bincode] version = "1" [features] serde1 = ["serde"] rand_xoshiro-0.4.0/LICENSE-APACHE010066400051560005156000000251371350313257000144000ustar0000000000000000 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. rand_xoshiro-0.4.0/LICENSE-MIT010066400051560005156000000020621350313257000141000ustar0000000000000000Copyright (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. rand_xoshiro-0.4.0/README.md010066400051560005156000000033031352275604500137340ustar0000000000000000# 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) [![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_xoshiro) [![API](https://docs.rs/rand_xoshiro/badge.svg)](https://docs.rs/rand_xoshiro) [![Minimum rustc version](https://img.shields.io/badge/rustc-1.32+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements) 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). Links: - [API documentation (master)](https://rust-random.github.io/rand/rand_xoshiro) - [API documentation (docs.rs)](https://docs.rs/rand_xoshiro) - [Changelog](https://github.com/rust-random/rand/blob/master/rand_xoshiro/CHANGELOG.md) ## Crate Features `rand_xoshiro` is no_std compatible by default. The `serde1` feature includes implementations of `Serialize` and `Deserialize` for the included RNGs. ## 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. rand_xoshiro-0.4.0/src/common.rs010066400051560005156000000161701353324246600151070ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. /// Initialize a RNG from a `u64` seed using `SplitMix64`. macro_rules! from_splitmix { ($seed:expr) => { { let mut rng = crate::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) } } /// Apply the ++ scrambler used by some RNGs from the xoshiro family. macro_rules! plusplus_u64 { ($x:expr, $y:expr, $rot:expr) => { $x.wrapping_add($y).rotate_left($rot).wrapping_add($x) } } /// Apply the ++ scrambler used by some RNGs from the xoshiro family. macro_rules! plusplus_u32 { ($x:expr, $y:expr) => { $x.wrapping_add($y).rotate_left(7).wrapping_add($x) } } /// 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 xoroshiro iteration for the ++ scrambler. macro_rules! impl_xoroshiro_u64_plusplus { ($self:expr) => { $self.s1 ^= $self.s0; $self.s0 = $self.s0.rotate_left(49) ^ $self.s1 ^ ($self.s1 << 21); $self.s1 = $self.s1.rotate_left(28); } } /// 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]); use core; impl Seed512 { /// Return an iterator over the seed. pub fn iter(&self) -> core::slice::Iter { self.0.iter() } } impl core::fmt::Debug for Seed512 { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::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 } } rand_xoshiro-0.4.0/src/lib.rs010066400051560005156000000126201353324246600143610ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! 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. //! - [`Xoshiro256PlusPlus`]: 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. //! - [`Xoroshiro128PlusPlus`]: An alternative to `Xoshiro256PlusPlus`, 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. //! - [`Xoshiro512PlusPlus`]: An alternative to `Xoshiro256PlusPlus` 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. //! - [`Xoshiro128PlusPlus`]: 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. //! //! The `*PlusPlus` generators perform similarily to the `*StarStar` generators. //! See the [xoshiro paper], where the differences are discussed in detail. //! //! [xoshiro]: http://xoshiro.di.unimi.it/ //! [xoshiro paper]: http://vigna.di.unimi.it/ftp/papers/ScrambledLinear.pdf //! [low linear complexity]: http://xoshiro.di.unimi.it/lowcomp.php #![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.4.0")] #![deny(missing_docs)] #![deny(missing_debug_implementations)] #![allow(clippy::unreadable_literal)] #![no_std] #[macro_use] mod common; mod splitmix64; mod xoshiro128starstar; mod xoshiro128plusplus; mod xoshiro128plus; mod xoshiro256starstar; mod xoshiro256plusplus; mod xoshiro256plus; mod xoshiro512starstar; mod xoshiro512plusplus; mod xoshiro512plus; mod xoroshiro128plus; mod xoroshiro128plusplus; mod xoroshiro128starstar; mod xoroshiro64starstar; mod xoroshiro64star; pub use rand_core; pub use splitmix64::SplitMix64; pub use xoshiro128starstar::Xoshiro128StarStar; pub use xoshiro128plusplus::Xoshiro128PlusPlus; pub use xoshiro128plus::Xoshiro128Plus; pub use xoshiro256starstar::Xoshiro256StarStar; pub use xoshiro256plusplus::Xoshiro256PlusPlus; pub use xoshiro256plus::Xoshiro256Plus; pub use common::Seed512; pub use xoshiro512starstar::Xoshiro512StarStar; pub use xoshiro512plusplus::Xoshiro512PlusPlus; pub use xoshiro512plus::Xoshiro512Plus; pub use xoroshiro128plus::Xoroshiro128Plus; pub use xoroshiro128starstar::Xoroshiro128StarStar; pub use xoroshiro128plusplus::Xoroshiro128PlusPlus; pub use xoroshiro64starstar::Xoroshiro64StarStar; pub use xoroshiro64star::Xoroshiro64Star; rand_xoshiro-0.4.0/src/splitmix64.rs010066400051560005156000000137611352300177400156370ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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 { SplitMix64::from_seed(seed.to_le_bytes()) } } #[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 : [u64 ; 50]= [ 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.iter() { 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 : [u32 ; 100]= [ 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.iter() { assert_eq!(rng.next_u32(), e); } } } rand_xoshiro-0.4.0/src/xoroshiro128plus.rs010066400051560005156000000077571352275604500170260ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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); } } } rand_xoshiro-0.4.0/src/xoroshiro128plusplus.rs010066400051560005156000000076371353324246600177260ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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 `xoroshiro128plusplus.c` /// reference source code](http://xoshiro.di.unimi.it/xoroshiro128plusplus.c) by /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro128PlusPlus { s0: u64, s1: u64, } impl Xoroshiro128PlusPlus { /// Jump forward, equivalently to 2^64 calls to `next_u64()`. /// /// This can be used to generate 2^64 non-overlapping subsequences for /// parallel computations. /// /// ``` /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoroshiro128PlusPlus; /// /// let rng1 = Xoroshiro128PlusPlus::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, [0x2bd7a6a6e99c2ddc, 0x0992ccaf6a6fca05]); } /// 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, [0x360fd5f2cf8d5d99, 0x9c6e6877736c46e3]); } } impl RngCore for Xoroshiro128PlusPlus { #[inline] fn next_u32(&mut self) -> u32 { self.next_u64() as u32 } #[inline] fn next_u64(&mut self) -> u64 { let r = plusplus_u64!(self.s0, self.s1, 17); impl_xoroshiro_u64_plusplus!(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 Xoroshiro128PlusPlus { type Seed = [u8; 16]; /// Create a new `Xoroshiro128PlusPlus`. If `seed` is entirely 0, it will be /// mapped to a different seed. fn from_seed(seed: [u8; 16]) -> Xoroshiro128PlusPlus { deal_with_zero_seed!(seed, Self); let mut s = [0; 2]; read_u64_into(&seed, &mut s); Xoroshiro128PlusPlus { s0: s[0], s1: s[1], } } /// Seed a `Xoroshiro128PlusPlus` from a `u64` using `SplitMix64`. fn seed_from_u64(seed: u64) -> Xoroshiro128PlusPlus { from_splitmix!(seed) } } #[cfg(test)] mod tests { use super::*; #[test] fn reference() { let mut rng = Xoroshiro128PlusPlus::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/xoshiro128plusplus.c let expected = [ 393217, 669327710093319, 1732421326133921491, 11394790081659126983, 9555452776773192676, 3586421180005889563, 1691397964866707553, 10735626796753111697, 15216282715349408991, 14247243556711267923, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); } } } rand_xoshiro-0.4.0/src/xoroshiro128starstar.rs010066400051560005156000000076021352275604500176730ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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); } } } rand_xoshiro-0.4.0/src/xoroshiro64star.rs010066400051560005156000000056541352275604500167250ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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 { from_splitmix!(seed) } } #[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); } } #[test] fn zero_seed() { let mut rng = Xoroshiro64Star::seed_from_u64(0); assert_ne!(rng.next_u64(), 0); } } rand_xoshiro-0.4.0/src/xoroshiro64starstar.rs010066400051560005156000000056461352275604500176200ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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` using `SplitMix64`. fn seed_from_u64(seed: u64) -> Xoroshiro64StarStar { from_splitmix!(seed) } } #[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); } } #[test] fn zero_seed() { let mut rng = Xoroshiro64StarStar::seed_from_u64(0); assert_ne!(rng.next_u64(), 0); } } rand_xoshiro-0.4.0/src/xoshiro128plus.rs010066400051560005156000000065521352302345000164410ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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); } } } rand_xoshiro-0.4.0/src/xoshiro128plusplus.rs010066400051560005156000000073711353324246600173600ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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 `xoshiro128plusplus.c` /// reference source code](http://xoshiro.di.unimi.it/xoshiro128plusplus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro128PlusPlus { s: [u32; 4], } impl Xoshiro128PlusPlus { /// Jump forward, equivalently to 2^64 calls to `next_u32()`. /// /// This can be used to generate 2^64 non-overlapping subsequences for /// parallel computations. /// /// ``` /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoroshiro128PlusPlus; /// /// let rng1 = Xoroshiro128PlusPlus::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]); } /// Jump forward, equivalently to 2^96 calls to `next_u32()`. /// /// 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!(u32, self, [0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662]); } } impl SeedableRng for Xoshiro128PlusPlus { type Seed = [u8; 16]; /// Create a new `Xoshiro128PlusPlus`. If `seed` is entirely 0, it will be /// mapped to a different seed. #[inline] fn from_seed(seed: [u8; 16]) -> Xoshiro128PlusPlus { deal_with_zero_seed!(seed, Self); let mut state = [0; 4]; read_u32_into(&seed, &mut state); Xoshiro128PlusPlus { s: state } } /// Seed a `Xoshiro128PlusPlus` from a `u64` using `SplitMix64`. fn seed_from_u64(seed: u64) -> Xoshiro128PlusPlus { from_splitmix!(seed) } } impl RngCore for Xoshiro128PlusPlus { #[inline] fn next_u32(&mut self) -> u32 { let result_starstar = plusplus_u32!(self.s[0], self.s[3]); 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 = Xoshiro128PlusPlus::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/xoshiro128plusplus.c let expected = [ 641, 1573767, 3222811527, 3517856514, 836907274, 4247214768, 3867114732, 1355841295, 495546011, 621204420, ]; for &e in &expected { assert_eq!(rng.next_u32(), e); } } } rand_xoshiro-0.4.0/src/xoshiro128starstar.rs010066400051560005156000000073571353324246600173400ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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]); } /// Jump forward, equivalently to 2^96 calls to `next_u32()`. /// /// 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!(u32, self, [0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662]); } } 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[1]); 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 (v1.1): // http://xoshiro.di.unimi.it/xoshiro128starstar.c let expected = [ 11520, 0, 5927040, 70819200, 2031721883, 1637235492, 1287239034, 3734860849, 3729100597, 4258142804, ]; for &e in &expected { assert_eq!(rng.next_u32(), e); } } } rand_xoshiro-0.4.0/src/xoshiro256plus.rs010066400051560005156000000101271352275604500164500ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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); } } } rand_xoshiro-0.4.0/src/xoshiro256plusplus.rs010066400051560005156000000100041353324246600173450ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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 `xoshiro256plusplus.c` /// reference source code](http://xoshiro.di.unimi.it/xoshiro256plusplus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro256PlusPlus { s: [u64; 4], } impl Xoshiro256PlusPlus { /// Jump forward, equivalently to 2^128 calls to `next_u64()`. /// /// This can be used to generate 2^128 non-overlapping subsequences for /// parallel computations. /// /// ``` /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoshiro256PlusPlus; /// /// let rng1 = Xoshiro256PlusPlus::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 Xoshiro256PlusPlus { type Seed = [u8; 32]; /// Create a new `Xoshiro256PlusPlus`. If `seed` is entirely 0, it will be /// mapped to a different seed. #[inline] fn from_seed(seed: [u8; 32]) -> Xoshiro256PlusPlus { deal_with_zero_seed!(seed, Self); let mut state = [0; 4]; read_u64_into(&seed, &mut state); Xoshiro256PlusPlus { s: state } } /// Seed a `Xoshiro256PlusPlus` from a `u64` using `SplitMix64`. fn seed_from_u64(seed: u64) -> Xoshiro256PlusPlus { from_splitmix!(seed) } } impl RngCore for Xoshiro256PlusPlus { #[inline] fn next_u32(&mut self) -> u32 { self.next_u64() as u32 } #[inline] fn next_u64(&mut self) -> u64 { let result_plusplus = plusplus_u64!(self.s[0], self.s[3], 23); impl_xoshiro_u64!(self); result_plusplus } #[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 = Xoshiro256PlusPlus::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/xoshiro256plusplus.c let expected = [ 41943041, 58720359, 3588806011781223, 3591011842654386, 9228616714210784205, 9973669472204895162, 14011001112246962877, 12406186145184390807, 15849039046786891736, 10450023813501588000, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); } } } rand_xoshiro-0.4.0/src/xoshiro256starstar.rs010066400051560005156000000077451352301543300173320ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; 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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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); } } } rand_xoshiro-0.4.0/src/xoshiro512plus.rs010066400051560005156000000104201353324246600164360ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; use rand_core::{SeedableRng, RngCore, Error}; use crate::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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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 ]); } /// Jump forward, equivalently to 2^384 calls to `next_u64()`. /// /// This can be used to generate 2^128 starting points, from each of which /// `jump()` will generate 2^128 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { impl_jump!(u64, self, [ 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a, 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17, 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5 ]); } } 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); } } } rand_xoshiro-0.4.0/src/xoshiro512plusplus.rs010066400051560005156000000105141353324246600173460ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; use rand_core::{SeedableRng, RngCore, Error}; use crate::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 `xoshiro512plusplus.c` /// reference source code](http://xoshiro.di.unimi.it/xoshiro512plusplus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro512PlusPlus { s: [u64; 8], } impl Xoshiro512PlusPlus { /// Jump forward, equivalently to 2^256 calls to `next_u64()`. /// /// This can be used to generate 2^256 non-overlapping subsequences for /// parallel computations. /// /// ``` /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoshiro512PlusPlus; /// /// let rng1 = Xoshiro512PlusPlus::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 ]); } /// Jump forward, equivalently to 2^384 calls to `next_u64()`. /// /// This can be used to generate 2^128 starting points, from each of which /// `jump()` will generate 2^128 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { impl_jump!(u64, self, [ 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a, 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17, 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5 ]); } } impl SeedableRng for Xoshiro512PlusPlus { type Seed = Seed512; /// Create a new `Xoshiro512PlusPlus`. If `seed` is entirely 0, it will be /// mapped to a different seed. #[inline] fn from_seed(seed: Seed512) -> Xoshiro512PlusPlus { deal_with_zero_seed!(seed, Self); let mut state = [0; 8]; read_u64_into(&seed.0, &mut state); Xoshiro512PlusPlus { s: state } } /// Seed a `Xoshiro512PlusPlus` from a `u64` using `SplitMix64`. fn seed_from_u64(seed: u64) -> Xoshiro512PlusPlus { from_splitmix!(seed) } } impl RngCore for Xoshiro512PlusPlus { #[inline] fn next_u32(&mut self) -> u32 { self.next_u64() as u32 } #[inline] fn next_u64(&mut self) -> u64 { let result_plusplus = plusplus_u64!(self.s[2], self.s[0], 17); impl_xoshiro_large!(self); result_plusplus } #[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 = Xoshiro512PlusPlus::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/xoshiro512plusplus.c let expected = [ 524291, 1048578, 539099140, 3299073855497, 6917532603230064654, 7494048333530275843, 14418333309547923463, 10960079161595355914, 18279570946505382726, 10209173166699159237, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); } } } rand_xoshiro-0.4.0/src/xoshiro512starstar.rs010066400051560005156000000104351353324246600173240ustar0000000000000000// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; use rand_core::{SeedableRng, RngCore, Error}; use crate::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)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] 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. /// /// ``` /// use rand_xoshiro::rand_core::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 ]); } /// Jump forward, equivalently to 2^384 calls to `next_u64()`. /// /// This can be used to generate 2^128 starting points, from each of which /// `jump()` will generate 2^128 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { impl_jump!(u64, self, [ 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a, 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17, 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5 ]); } } 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); } } } rand_xoshiro-0.4.0/tests/serde.rs010066400051560005156000000033311352275604500152700ustar0000000000000000#![cfg(feature="serde1")] use rand_core::{RngCore, SeedableRng}; use rand_xoshiro::{SplitMix64, Xoroshiro64StarStar, Xoroshiro64Star, Xoroshiro128Plus, Xoroshiro128StarStar, Xoshiro128StarStar, Xoshiro128Plus, Xoshiro256StarStar, Xoshiro256Plus, Xoshiro512StarStar, Xoshiro512Plus}; macro_rules! serde_rng { ($rng:ident) => { use bincode; use std::io::{BufWriter, BufReader}; let mut rng = $rng::seed_from_u64(0); let buf: Vec = Vec::new(); let mut buf = BufWriter::new(buf); bincode::serialize_into(&mut buf, &rng).expect("Could not serialize"); let buf = buf.into_inner().unwrap(); let mut read = BufReader::new(&buf[..]); let mut deserialized: $rng = bincode::deserialize_from(&mut read) .expect("Could not deserialize"); for _ in 0..16 { assert_eq!(rng.next_u64(), deserialized.next_u64()); } } } #[test] fn test_splitmix64() { serde_rng!(SplitMix64); } #[test] fn test_xoroshiro64starstar() { serde_rng!(Xoroshiro64StarStar); } #[test] fn test_xoroshiro64star() { serde_rng!(Xoroshiro64Star); } #[test] fn test_xoroshiro128plus() { serde_rng!(Xoroshiro128Plus); } #[test] fn test_xoroshiro128starstar() { serde_rng!(Xoroshiro128StarStar); } #[test] fn test_xoshiro128starstar() { serde_rng!(Xoshiro128StarStar); } #[test] fn test_xoshiro128plus() { serde_rng!(Xoshiro128Plus); } #[test] fn test_xoshiro256starstar() { serde_rng!(Xoshiro256StarStar); } #[test] fn test_xoshiro256plus() { serde_rng!(Xoshiro256Plus); } #[test] fn test_xoshiro512starstar() { serde_rng!(Xoshiro512StarStar); } #[test] fn test_xoshiro512plus() { serde_rng!(Xoshiro512Plus); } rand_xoshiro-0.4.0/.cargo_vcs_info.json0000644000000001120000000000000135650ustar00{ "git": { "sha1": "2c43268c8260cad21d171b94807e6cd8b96e50b7" } }