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
impl Counter
Sourcepub fn new(step: u64, substep: u32, seed: u32) -> Self
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);Sourcepub fn indices(self, a: u32, b: u32) -> Self
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));