#[repr(transparent)]pub struct Xorshift64(pub u64);Expand description
A pseudo-random number generator (PRNG) that uses a [Xorshift algorithm]1
to generate 64 bits of randomness at a time, represented by a u64.
Xorshift is a type of linear-feedback shift register that uses only three right-shifts and three xor operations per generated number, making it very efficient. Xorshift64 has a period of 264-1: it yields every number in the interval [1, 264) exactly once before repeating.
Marsaglia, G. (2003). Xorshift RNGs. Journal of Statistical Software, 8(14), 1–6. https://doi.org/10.18637/jss.v008.i14 ↩
Tuple Fields§
§0: u64Implementations§
Source§impl Xorshift64
impl Xorshift64
Sourcepub const DEFAULT_SEED: u64 = 378_682_147_834_061u64
pub const DEFAULT_SEED: u64 = 378_682_147_834_061u64
A random 64-bit prime, used to initialize the generator returned by
Xorshift64::default().
Sourcepub fn from_seed(seed: u64) -> Self
pub fn from_seed(seed: u64) -> Self
Returns a new Xorshift64 seeded by the given number.
Two Xorshift64 instances generate the same sequence of pseudo-random
numbers if and only if they were created with the same seed.
(Technically, every Xorshift64 instance yields values from the same
sequence; the seed determines the starting point in the sequence).
§Examples
use retrofire_core::math::rand::Xorshift64;
let mut g = Xorshift64::from_seed(123);
assert_eq!(g.next_bits(), 133101616827);
assert_eq!(g.next_bits(), 12690785413091508870);
assert_eq!(g.next_bits(), 7516749944291143043);§Panics
If seed equals 0.
Sourcepub fn from_time() -> Self
pub fn from_time() -> Self
Returns a new Xorshift64 seeded by the current system time.
Note that depending on the precision of the system clock, two or more calls to this function in quick succession may return instances seeded by the same number.
§Examples
use std::thread;
use retrofire_core::math::rand::Xorshift64;
let mut g = Xorshift64::from_time();
thread::sleep_ms(1); // Just to be sure
let mut h = Xorshift64::from_time();
assert_ne!(g.next_bits(), h.next_bits());Trait Implementations§
Source§impl Clone for Xorshift64
impl Clone for Xorshift64
Source§fn clone(&self) -> Xorshift64
fn clone(&self) -> Xorshift64
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Xorshift64
impl Debug for Xorshift64
Source§impl Default for Xorshift64
impl Default for Xorshift64
Source§fn default() -> Self
fn default() -> Self
Returns a Xorshift64 seeded with DEFAULT_SEED.
§Examples
use retrofire_core::math::rand::Xorshift64;
let mut g = Xorshift64::default();
assert_eq!(g.next_bits(), 11039719294064252060);