| 1 | // Copyright 2018-2023 Developers of the Rand project. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | //! This crate implements the [xoshiro] family of pseudorandom number generators |
| 10 | //! designed by David Blackman and Sebastiano Vigna. They feature high |
| 11 | //! performance and a small state and supersede the previous xorshift-based |
| 12 | //! generators. However, they are not cryptographically secure and their output |
| 13 | //! can be predicted by observing a few samples. |
| 14 | //! |
| 15 | //! The following generators are implemented: |
| 16 | //! |
| 17 | //! # 64-bit generators |
| 18 | //! - [`Xoshiro256StarStar`]: Recommended for all purposes. Excellent speed and |
| 19 | //! a state space (256 bits) large enough for any parallel application. |
| 20 | //! - [`Xoshiro256PlusPlus`]: Recommended for all purposes. Excellent speed and |
| 21 | //! a state space (256 bits) large enough for any parallel application. |
| 22 | //! - [`Xoshiro256Plus`]: Recommended for generating 64-bit floating-point |
| 23 | //! numbers. About 15% faster than `Xoshiro256StarStar`, but has a [low linear |
| 24 | //! complexity] in the lowest bits (which are discarded when generating |
| 25 | //! floats), making it fail linearity tests. This is unlikely to have any |
| 26 | //! impact in practise. |
| 27 | //! - [`Xoroshiro128StarStar`]: An alternative to `Xoshiro256StarStar`, having |
| 28 | //! the same speed but using half the state. Only suited for low-scale parallel |
| 29 | //! applications. |
| 30 | //! - [`Xoroshiro128PlusPlus`]: An alternative to `Xoshiro256PlusPlus`, having |
| 31 | //! the same speed but using half the state. Only suited for low-scale parallel |
| 32 | //! applications. |
| 33 | //! - [`Xoroshiro128Plus`]: An alternative to `Xoshiro256Plus`, having the same |
| 34 | //! speed but using half the state. Only suited for low-scale parallel |
| 35 | //! applications. Has a [low linear complexity] in the lowest bits (which are |
| 36 | //! discarded when generating floats), making it fail linearity tests. This is |
| 37 | //! unlikely to have any impact in practise. |
| 38 | //! - [`Xoshiro512StarStar`]: An alternative to `Xoshiro256StarStar` with more |
| 39 | //! state and the same speed. |
| 40 | //! - [`Xoshiro512PlusPlus`]: An alternative to `Xoshiro256PlusPlus` with more |
| 41 | //! state and the same speed. |
| 42 | //! - [`Xoshiro512Plus`]: An alternative to `Xoshiro512Plus` with more |
| 43 | //! state and the same speed. Has a [low linear complexity] in the lowest bits |
| 44 | //! (which are discarded when generating floats), making it fail linearity |
| 45 | //! tests. This is unlikely to have any impact in practise. |
| 46 | //! - [`SplitMix64`]: Recommended for initializing generators of the xoshiro |
| 47 | //! family from a 64-bit seed. Used for implementing `seed_from_u64`. |
| 48 | //! |
| 49 | //! # 32-bit generators |
| 50 | //! - [`Xoshiro128StarStar`]: Recommended for all purposes. Excellent speed. |
| 51 | //! - [`Xoshiro128PlusPlus`]: Recommended for all purposes. Excellent speed. |
| 52 | //! - [`Xoshiro128Plus`]: Recommended for generating 32-bit floating-point |
| 53 | //! numbers. Faster than `Xoshiro128StarStar`, but has a [low linear |
| 54 | //! complexity] in the lowest bits (which are discarded when generating |
| 55 | //! floats), making it fail linearity tests. This is unlikely to have any |
| 56 | //! impact in practise. |
| 57 | //! - [`Xoroshiro64StarStar`]: An alternative to `Xoshiro128StarStar`, having |
| 58 | //! the same speed but using half the state. |
| 59 | //! - [`Xoroshiro64Star`]: An alternative to `Xoshiro128Plus`, having the |
| 60 | //! same speed but using half the state. Has a [low linear complexity] in the |
| 61 | //! lowest bits (which are discarded when generating floats), making it fail |
| 62 | //! linearity tests. This is unlikely to have any impact in practise. |
| 63 | //! |
| 64 | //! The `*PlusPlus` generators perform similarly to the `*StarStar` generators. |
| 65 | //! See the [xoshiro paper], where the differences are discussed in detail. |
| 66 | //! |
| 67 | //! # Example |
| 68 | //! |
| 69 | //! To initialize a generator, use the [`SeedableRng`][rand_core::SeedableRng] trait: |
| 70 | //! |
| 71 | //! ``` |
| 72 | //! use rand_core::{SeedableRng, RngCore}; |
| 73 | //! use rand_xoshiro::Xoshiro256PlusPlus; |
| 74 | //! |
| 75 | //! let mut rng = Xoshiro256PlusPlus::seed_from_u64(0); |
| 76 | //! let x = rng.next_u64(); |
| 77 | //! ``` |
| 78 | //! |
| 79 | //! [xoshiro]: http://xoshiro.di.unimi.it/ |
| 80 | //! [xoshiro paper]: http://vigna.di.unimi.it/ftp/papers/ScrambledLinear.pdf |
| 81 | //! [low linear complexity]: http://xoshiro.di.unimi.it/lowcomp.php |
| 82 | |
| 83 | #![doc ( |
| 84 | html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" , |
| 85 | html_favicon_url = "https://www.rust-lang.org/favicon.ico" , |
| 86 | html_root_url = "https://docs.rs/rand_xoshiro/0.7.0" |
| 87 | )] |
| 88 | #![forbid (unsafe_code)] |
| 89 | #![deny (missing_docs)] |
| 90 | #![deny (missing_debug_implementations)] |
| 91 | #![allow (clippy::unreadable_literal)] |
| 92 | #![no_std ] |
| 93 | |
| 94 | #[macro_use ] |
| 95 | mod common; |
| 96 | mod splitmix64; |
| 97 | mod xoroshiro128plus; |
| 98 | mod xoroshiro128plusplus; |
| 99 | mod xoroshiro128starstar; |
| 100 | mod xoroshiro64star; |
| 101 | mod xoroshiro64starstar; |
| 102 | mod xoshiro128plus; |
| 103 | mod xoshiro128plusplus; |
| 104 | mod xoshiro128starstar; |
| 105 | mod xoshiro256plus; |
| 106 | mod xoshiro256plusplus; |
| 107 | mod xoshiro256starstar; |
| 108 | mod xoshiro512plus; |
| 109 | mod xoshiro512plusplus; |
| 110 | mod xoshiro512starstar; |
| 111 | |
| 112 | pub use common::Seed512; |
| 113 | pub use rand_core; |
| 114 | pub use splitmix64::SplitMix64; |
| 115 | pub use xoroshiro128plus::Xoroshiro128Plus; |
| 116 | pub use xoroshiro128plusplus::Xoroshiro128PlusPlus; |
| 117 | pub use xoroshiro128starstar::Xoroshiro128StarStar; |
| 118 | pub use xoroshiro64star::Xoroshiro64Star; |
| 119 | pub use xoroshiro64starstar::Xoroshiro64StarStar; |
| 120 | pub use xoshiro128plus::Xoshiro128Plus; |
| 121 | pub use xoshiro128plusplus::Xoshiro128PlusPlus; |
| 122 | pub use xoshiro128starstar::Xoshiro128StarStar; |
| 123 | pub use xoshiro256plus::Xoshiro256Plus; |
| 124 | pub use xoshiro256plusplus::Xoshiro256PlusPlus; |
| 125 | pub use xoshiro256starstar::Xoshiro256StarStar; |
| 126 | pub use xoshiro512plus::Xoshiro512Plus; |
| 127 | pub use xoshiro512plusplus::Xoshiro512PlusPlus; |
| 128 | pub use xoshiro512starstar::Xoshiro512StarStar; |
| 129 | |