| Taskflow: A General-purpose Task-parallel Programming System

Loading...
Searching...
No Matches
Public Member Functions | List of all members
tf::Xorshift< T > Class Template Reference

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

`#include <taskflow/utility/math.hpp>`

## Public Member Functions
| Xorshift ()=default
| constructs an uninitialized xor-shift generator
| Xorshift (T value)
| constructs a xor-shift generator with the given seed
void | seed (T value)
| seeds the generator with a new value
T | operator() ()
| generates the next pseudo-random value

## Detailed Description

template<typename T>
class tf::Xorshift< T >

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

Template Parameters

  T | unsigned integral type used as the internal state (supported uint32_t and uint64_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.

Note
  The internal state must be seeded with a non-zero value. This class is not thread-safe. Each thread should maintain its own instance.

## Constructor & Destructor Documentation

## ◆ Xorshift() [1/2]

template<typename T >

tf::Xorshift< T >::Xorshift | ( |  | ) |
| default

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.

tf::Xorshift<uint64_t> rng;
rng.seed(12345);
tf::Xorshift
class to create a fast xorshift-based pseudo-random number generator
Definition math.hpp:412
tf::Xorshift::seed
void seed(T value)
seeds the generator with a new value
Definition math.hpp:457

## ◆ Xorshift() [2/2]

template<typename T >

tf::Xorshift< T >::Xorshift | ( | T | value | ) |
| inline

constructs a xor-shift generator with the given seed

Parameters

  value | the new seed value to use

The seed value must be non-zero.

tf::Xorshift<uint64_t> rng(12345);
auto r = rng();

## Member Function Documentation

## ◆ operator()()

template<typename T >

T tf::Xorshift< T >::operator() | ( |  | ) |
| inline

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.

tf::Xorshift<uint64_t> rng(12345);
uint64_t r1 = rng();
uint64_t r2 = rng();

Warning
  Calling this function before seeding the generator with a non-zero value results in undefined behavior.

## ◆ seed()

template<typename T >

void tf::Xorshift< T >::seed | ( | T | value | ) |
| inline

seeds the generator with a new value

Parameters

  value | the new seed value

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

tf::Xorshift<uint64_t> rng;
rng.seed(12345);

The documentation for this class was generated from the following file:

- taskflow/utility/math.hpp