Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb::internal::FastRandom Class Reference

A fast random number generator. More...

#include <tbb_misc.h>

Collaboration diagram for tbb::internal::FastRandom:

Public Member Functions

unsigned short get ()
 Get a random number. More...
 
unsigned short get (unsigned &seed)
 Get a random number for the given seed; update the seed for next use. More...
 
 FastRandom (void *unique_ptr)
 Construct a random number generator. More...
 
 FastRandom (uint32_t seed)
 
 FastRandom (uint64_t seed)
 
template<typename T >
void init (T seed)
 
void init (uint64_t seed, int_to_type< 8 >)
 
void init (uint32_t seed, int_to_type< 4 >)
 

Private Attributes

unsigned x
 
unsigned c
 

Static Private Attributes

static const unsigned a = 0x9e3779b1
 

Detailed Description

A fast random number generator.

Uses linear congruential method.

Definition at line 140 of file tbb_misc.h.

Constructor & Destructor Documentation

◆ FastRandom() [1/3]

tbb::internal::FastRandom::FastRandom ( void unique_ptr)
inline

Construct a random number generator.

Definition at line 162 of file tbb_misc.h.

162 { init(uintptr_t(unique_ptr)); }

References init().

Here is the call graph for this function:

◆ FastRandom() [2/3]

tbb::internal::FastRandom::FastRandom ( uint32_t  seed)
inline

Definition at line 163 of file tbb_misc.h.

163 { init(seed); }

References init().

Here is the call graph for this function:

◆ FastRandom() [3/3]

tbb::internal::FastRandom::FastRandom ( uint64_t  seed)
inline

Definition at line 164 of file tbb_misc.h.

164 { init(seed); }

References init().

Here is the call graph for this function:

Member Function Documentation

◆ get() [1/2]

unsigned short tbb::internal::FastRandom::get ( )
inline

Get a random number.

Definition at line 151 of file tbb_misc.h.

151  {
152  return get(x);
153  }

References x.

Referenced by tbb::internal::random_lane_selector::operator()(), tbb::internal::task_stream< num_priority_levels >::push(), and tbb::internal::generic_scheduler::steal_task().

Here is the caller graph for this function:

◆ get() [2/2]

unsigned short tbb::internal::FastRandom::get ( unsigned &  seed)
inline

Get a random number for the given seed; update the seed for next use.

Definition at line 155 of file tbb_misc.h.

155  {
156  unsigned short r = (unsigned short)(seed>>16);
157  __TBB_ASSERT(c&1, "c must be odd for big rng period");
158  seed = seed*a+c;
159  return r;
160  }

References __TBB_ASSERT, a, and c.

◆ init() [1/3]

template<typename T >
void tbb::internal::FastRandom::init ( seed)
inline

Definition at line 166 of file tbb_misc.h.

166  {
167  init(seed,int_to_type<sizeof(seed)>());
168  }

Referenced by FastRandom(), and init().

Here is the caller graph for this function:

◆ init() [2/3]

void tbb::internal::FastRandom::init ( uint32_t  seed,
int_to_type< 4 >   
)
inline

Definition at line 172 of file tbb_misc.h.

172  {
173 #if __TBB_OLD_PRIMES_RNG
174  x = seed;
175  a = GetPrime( seed );
176 #else
177  // threads use different seeds for unique sequences
178  c = (seed|1)*0xba5703f5; // c must be odd, shuffle by a prime number
179  x = c^(seed>>1); // also shuffle x for the first get() invocation
180 #endif
181  }

References a, c, tbb::internal::GetPrime(), and x.

Here is the call graph for this function:

◆ init() [3/3]

void tbb::internal::FastRandom::init ( uint64_t  seed,
int_to_type< 8 >   
)
inline

Definition at line 169 of file tbb_misc.h.

169  {
170  init(uint32_t((seed>>32)+seed), int_to_type<4>());
171  }

References init().

Here is the call graph for this function:

Member Data Documentation

◆ a

const unsigned tbb::internal::FastRandom::a = 0x9e3779b1
staticprivate

Definition at line 147 of file tbb_misc.h.

Referenced by get(), and init().

◆ c

unsigned tbb::internal::FastRandom::c
private

Definition at line 146 of file tbb_misc.h.

Referenced by get(), and init().

◆ x

unsigned tbb::internal::FastRandom::x
private

Definition at line 146 of file tbb_misc.h.

Referenced by get(), and init().


The documentation for this class was generated from the following file:
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::internal::FastRandom::get
unsigned short get()
Get a random number.
Definition: tbb_misc.h:151
tbb::internal::FastRandom::a
static const unsigned a
Definition: tbb_misc.h:147
tbb::internal::GetPrime
unsigned GetPrime(unsigned seed)
tbb::internal::FastRandom::c
unsigned c
Definition: tbb_misc.h:146
tbb::internal::FastRandom::x
unsigned x
Definition: tbb_misc.h:146
tbb::internal::FastRandom::init
void init(T seed)
Definition: tbb_misc.h:166

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.