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 135 of file tbb_misc.h.

Constructor & Destructor Documentation

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

Construct a random number generator.

Definition at line 157 of file tbb_misc.h.

157 { init(uintptr_t(unique_ptr)); }
tbb::internal::FastRandom::FastRandom ( uint32_t  seed)
inline

Definition at line 158 of file tbb_misc.h.

158 { init(seed); }
tbb::internal::FastRandom::FastRandom ( uint64_t  seed)
inline

Definition at line 159 of file tbb_misc.h.

159 { init(seed); }

Member Function Documentation

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

Get a random number.

Definition at line 146 of file tbb_misc.h.

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

146  {
147  return get(x);
148  }

Here is the caller graph for this function:

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 150 of file tbb_misc.h.

References __TBB_ASSERT.

150  {
151  unsigned short r = (unsigned short)(seed>>16);
152  __TBB_ASSERT(c&1, "c must be odd for big rng period");
153  seed = seed*a+c;
154  return r;
155  }
static const unsigned a
Definition: tbb_misc.h:142
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
template<typename T >
void tbb::internal::FastRandom::init ( seed)
inline

Definition at line 161 of file tbb_misc.h.

161  {
162  init(seed,int_to_type<sizeof(seed)>());
163  }
void tbb::internal::FastRandom::init ( uint64_t  seed,
int_to_type< 8 >   
)
inline

Definition at line 164 of file tbb_misc.h.

164  {
165  init(uint32_t((seed>>32)+seed), int_to_type<4>());
166  }
void tbb::internal::FastRandom::init ( uint32_t  seed,
int_to_type< 4 >   
)
inline

Definition at line 167 of file tbb_misc.h.

References tbb::internal::GetPrime().

167  {
168 #if __TBB_OLD_PRIMES_RNG
169  x = seed;
170  a = GetPrime( seed );
171 #else
172  // threads use different seeds for unique sequences
173  c = (seed|1)*0xba5703f5; // c must be odd, shuffle by a prime number
174  x = c^(seed>>1); // also shuffle x for the first get() invocation
175 #endif
176  }
unsigned GetPrime(unsigned seed)
static const unsigned a
Definition: tbb_misc.h:142

Here is the call graph for this function:

Member Data Documentation

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

Definition at line 142 of file tbb_misc.h.

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

Definition at line 141 of file tbb_misc.h.

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

Definition at line 141 of file tbb_misc.h.


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

Copyright © 2005-2020 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.