Skip to main content

Counter

Struct Counter 

Source
pub struct Counter { /* private fields */ }
Expand description

Conveniently construct counter based random number generators.

Counter based random number generators produce a stream of random numbers that is a reproducible function of a counter value. They are efficient to use, even when only one or a few random numbers are needed. Counter allows callers to conveniently construct RNGS that are independent, as well as ones that produce identical values.

There are 3 required elements of each counter.

  • step (8 bytes) is the current simulation step and ensures that random number streams are not correlated from one simulation step to the next.
  • substep (4 bytes) similarly ensures that different parts of the algorithm that advance the simulation are not correlated within a single step.
  • seed (4 bytes) is a value that allows users to execute replicate simulations that are identical except for the random numbers applied.

There is an additional 8-byte index. Generally, many simulation algorithms will set this to particle indices so that RNG streams are independent from one particle (or pair of particles) to the next. The indices method treats the index as two 4-byte indices. To generate the same random numbers (e.g. for use in a DPD thermostat) in independent threads, set the first index to min(i,j) and the second to max(i,j).

§Performance

The current implementation uses SFC64, which generates one 64-bit word at at time. Benchmarks show that executing Counter.new(...).make_rng() and sampling values that fall in the first batch runs at approximately 100 million operations per second (run cargo bench to see the measured performance on your architecture).

§Example

use hoomd_rand::Counter;
use rand::{Rng, RngExt};

let mut rng = Counter::new(step, substep, seed).make_rng();

let r: f64 = rng.random();

Implementations§

Source§

impl Counter

Source

pub fn new(step: u64, substep: u32, seed: u32) -> Self

Construct a new counter.

On constructions, the index defaults to 0.

§Example
use hoomd_rand::Counter;

let step = 100_000;
let substep = 10;
let seed = 100;

let counter = Counter::new(step, substep, seed);
Source

pub fn indices(self, a: u32, b: u32) -> Self

Set the index with two 4-byte values.

§Example
use hoomd_rand::Counter;

let step = 100_000;
let substep = 10;
let seed = 100;
let i = 12;
let j = 152;

let counter = Counter::new(step, substep, seed).indices(i.max(j), i.min(j));
Source

pub fn index(self, index: u64) -> Self

Set the index.

§Example
use hoomd_rand::Counter;

let step = 100_000;
let substep = 10;
let seed = 100;
let index = 1_000_000_000_000_u64;

let counter = Counter::new(step, substep, seed).index(index);
Source

pub fn make_rng(self) -> impl Rng + use<>

Seed a Rng with the counter.

§Example
use hoomd_rand::Counter;
use rand::{Rng, RngExt};

let step = 100_000;
let substep = 10;
let seed = 100;

let mut rng = Counter::new(step, substep, seed).make_rng();

let r: f64 = rng.random();

Trait Implementations§

Source§

impl Clone for Counter

Source§

fn clone(&self) -> Counter

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 Counter

Source§

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

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

impl<'de> Deserialize<'de> for Counter

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Counter

Source§

fn eq(&self, other: &Counter) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Counter

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Counter

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,