Xorshift64

Struct Xorshift64 

Source
#[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.


  1. Marsaglia, G. (2003). Xorshift RNGs. Journal of Statistical Software, 8(14), 1–6. https://doi.org/10.18637/jss.v008.i14 

Tuple Fields§

§0: u64

Implementations§

Source§

impl Xorshift64

Source

pub const DEFAULT_SEED: u64 = 378_682_147_834_061u64

A random 64-bit prime, used to initialize the generator returned by Xorshift64::default().

Source

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.

Source

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());
Source

pub fn next_bits(&mut self) -> u64

Returns 64 bits of pseudo-randomness.

Successive calls to this function (with the same self) will yield every value in the interval [1, 264) exactly once before starting to repeat the sequence.

Trait Implementations§

Source§

impl Clone for Xorshift64

Source§

fn clone(&self) -> Xorshift64

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Xorshift64

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Xorshift64

Source§

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);
Source§

impl Copy for Xorshift64

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.