Back to Taskflow

template<typename T> tf::Xorshift class

docs/classtf_1_1Xorshift.html

4.0.02.3 KB
Original Source

template<typename T> tf::Xorshift class

class to create a fast xorshift-based pseudo-random number generator

Template parameters
T

This class implements a lightweight xorshift pseudo-random number generator suitable for performance-critical paths such as schedulers, work-stealing victim selection, and randomized backoff. The implementation is branchless on the hot path and has a very small state footprint (one machine word). All operations are integer-only.

Constructors, destructors, conversion operators

Xorshift() defaultedconstructs an uninitialized xor-shift generatorXorshift(T value)constructs a xor-shift generator with the given seed

Public functions

void seed(T value)seeds the generator with a new valueauto operator()() -> Tgenerates the next pseudo-random value

Function documentation

template<typename T> tf::Xorshift<T>::Xorshift() defaulted

constructs an uninitialized xor-shift generator

The internal state is not initialized. The user must call seed() with a non-zero value before generating numbers.

template<typename T> tf::Xorshift<T>::Xorshift(T value)

constructs a xor-shift generator with the given seed

Parameters
value

The seed value must be non-zero.

template<typename T> void tf::Xorshift<T>::seed(T value)

seeds the generator with a new value

Parameters
value

The seed value must be non-zero. A zero seed results in a degenerated generator that always returns zero.

template<typename T> T tf::Xorshift<T>::operator()()

generates the next pseudo-random value

| Returns | a pseudo-random value of type T |

For 32-bit state, this function implements the Xorshift32 algorithm. For 64-bit state, this function implements the Xorshift64 algorithm with a multiplicative output transformation to improve distribution.