ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RandomGenerators.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File RandomGenerators.h -------------------------*/
3/*--------------------------------------------------------------------------*/
149/*--------------------------------------------------------------------------*/
150/*----------------------------- DEFINITIONS --------------------------------*/
151/*--------------------------------------------------------------------------*/
152
153#ifndef __ADS_RandomGenerators
154 #define __ADS_RandomGenerators
155/* self-identification: #endif constant */
156
157/*--------------------------------------------------------------------------*/
158/*------------------------------ INCLUDES ----------------------------------*/
159/*--------------------------------------------------------------------------*/
160
161#include "ADS/Contract.h"
162#include "ADS/Portable.h"
165
166#include <array>
167#include <bit>
168#include <cmath>
169#include <concepts>
170#include <cstddef>
171#include <cstdint>
172#include <iterator>
173#include <limits>
174#include <utility>
175#include <vector>
176
177// xoshiro256++ and xoshiro256** of Boost.Random (Boost 1.86 or later), with
178// ADS_WITH_BOOST
179#if defined( ADS_WITH_BOOST ) && ! defined( ADS_RANDOM_BOOST )
180 #include <boost/version.hpp>
181 #if BOOST_VERSION >= 108600
182 #define ADS_RANDOM_BOOST 1
183 #endif
184#endif
185#ifndef ADS_RANDOM_BOOST
186 #define ADS_RANDOM_BOOST 0
187#endif
188#if ADS_RANDOM_BOOST
189 #include <type_traits>
190
191 #include <boost/random/xoshiro.hpp>
192#endif
193
194/*--------------------------------------------------------------------------*/
195/*--------------------------- NAMESPACE ------------------------------------*/
196/*--------------------------------------------------------------------------*/
197
200#define ADS_RNG_INLINE ADS_ALWAYS_INLINE
201
202namespace ADS
203{
204
207template< class G >
209 std::uniform_random_bit_generator< std::remove_reference_t< G > > &&
210 std::same_as< typename std::remove_reference_t< G >::result_type ,
211 std::uint64_t > &&
212 ( std::remove_reference_t< G >::min() == 0 ) &&
213 ( std::remove_reference_t< G >::max() ==
214 std::numeric_limits< std::uint64_t >::max() );
215
216/*--------------------------------------------------------------------------*/
217/*--------------------------- CLASS Xoshiro256 -----------------------------*/
218/*--------------------------------------------------------------------------*/
219
220#if ! ADS_RANDOM_BOOST
223template< bool StarStar >
225{
226 public:
227 using result_type = std::uint64_t;
228
230 explicit constexpr Xoshiro256T( std::uint64_t seed = 1 );
231
249 constexpr std::uint64_t operator()( void );
250
266 constexpr void jump( void );
267
269 static constexpr std::uint64_t min( void ) { return( 0 ); }
271 static constexpr std::uint64_t max( void ) {
272 return( std::numeric_limits< std::uint64_t >::max() );
273 }
274
276 bool operator==( const Xoshiro256T & ) const = default;
277
278 private:
279 std::uint64_t f_s[ 4 ];
280 }; // end( class Xoshiro256T )
281#else
284template< bool StarStar >
285class Xoshiro256T
286{
288 using G = std::conditional_t< StarStar , boost::random::xoshiro256mm ,
289 boost::random::xoshiro256pp >;
290
291 public:
292 using result_type = std::uint64_t;
293
296 explicit Xoshiro256T( std::uint64_t seed = 1 );
297
299 std::uint64_t operator()( void ) { return( f_g() ); }
300
303 void jump( void ) { f_g.jump(); }
304
306 static constexpr std::uint64_t min( void ) { return( 0 ); }
308 static constexpr std::uint64_t max( void ) {
309 return( std::numeric_limits< std::uint64_t >::max() );
310 }
311
313 bool operator==( const Xoshiro256T & o ) const { return( f_g == o.f_g ); }
314
315 private:
316 G f_g;
317 }; // end( class Xoshiro256T )
318#endif
319
322
323/*--------------------------------------------------------------------------*/
324/*----------------------------- CLASS WyRand -------------------------------*/
325/*--------------------------------------------------------------------------*/
326
329{
330 public:
331 using result_type = std::uint64_t;
332
334 explicit constexpr WyRand( std::uint64_t seed = 1 ) : f_s( seed ) { }
335
349 constexpr std::uint64_t operator()( void );
350
352 static constexpr std::uint64_t min( void ) { return( 0 ); }
354 static constexpr std::uint64_t max( void ) {
355 return( std::numeric_limits< std::uint64_t >::max() );
356 }
357
359 bool operator==( const WyRand & ) const = default;
360
361 private:
362 std::uint64_t f_s;
363 }; // end( class WyRand )
364
365/*--------------------------------------------------------------------------*/
366/*----------------------------- CLASS PhiloxT ------------------------------*/
367/*--------------------------------------------------------------------------*/
368
372template< class W >
374{
375 static_assert( std::same_as< W , std::uint32_t > ||
376 std::same_as< W , std::uint64_t > ,
377 "words of 32 or 64 bits" );
378
379 public:
380 using result_type = W;
381 using Block = std::array< W , 4 >;
382 using Key = std::array< W , 2 >;
383
385 static constexpr W default_seed = 20111115u;
386
388 explicit constexpr PhiloxT( W seed = default_seed );
389
392 constexpr W operator()( void );
393
409 constexpr void discard( unsigned long long z );
410
413 constexpr void setCounter( const Block & c );
414
433 static constexpr Block block( Block c , Key k );
434
436 static constexpr W min( void ) { return( 0 ); }
438 static constexpr W max( void ) { return( std::numeric_limits< W >::max() ); }
439
441 bool operator==( const PhiloxT & ) const = default;
442
443 private:
446 static constexpr unsigned lanes = 2;
447
449 constexpr void refill( void );
450
451 Key f_key;
452 Block f_ctr{};
453 W f_buf[ 4 * lanes ] = {};
454 unsigned f_at = 4 * lanes;
455 }; // end( class PhiloxT )
456
459
460/*--------------------------------------------------------------------------*/
461/*--------------------------- CLASS AliasTable -----------------------------*/
462/*--------------------------------------------------------------------------*/
463
467{
468 public:
491 explicit AliasTable( const std::vector< double > & w );
492
494 [[nodiscard]] std::size_t size( void ) const { return( f_T.size() ); }
495
510 template< FullWordGenerator G >
511 std::size_t operator()( G && g ) const;
512
515 [[nodiscard]] double probability( std::size_t i ) const;
516
517 private:
519 struct Entry
520 {
521 std::uint64_t keep;
522 std::uint32_t alias;
523 };
524 std::vector< Entry > f_T;
525 }; // end( class AliasTable )
526
527/*--------------------------------------------------------------------------*/
528/*------------------------------ FUNCTIONS ---------------------------------*/
529/*--------------------------------------------------------------------------*/
530
549template< FullWordGenerator G >
550std::uint64_t uniformBelow( G && g , std::uint64_t s );
551
554template< FullWordGenerator G >
555double uniformReal( G && g );
556
579template< FullWordGenerator G >
581
600template< FullWordGenerator G >
602
626template< std::random_access_iterator I , FullWordGenerator G >
627void shuffleFast( I first , I last , G && g );
628
629/*--------------------------------------------------------------------------*/
630
631} // end( namespace ADS )
632
633/*--------------------------------------------------------------------------*/
634/*-------------------------- IMPLEMENTATION --------------------------------*/
635/*--------------------------------------------------------------------------*/
636
637#include "ADS/numeric/impl/RandomGenerators.tpp"
638
639/*--------------------------------------------------------------------------*/
640
641#endif /* RandomGenerators.h included */
642
643/*--------------------------------------------------------------------------*/
644/*----------------------- End File RandomGenerators.h ----------------------*/
645/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits:
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
Hashing with random tables, beyond the book:
std::size_t size(void) const
the number of the items
Definition RandomGenerators.h:494
double probability(std::size_t i) const
the probability of , as the table gives it
std::size_t operator()(G &&g) const
an index drawn with probability proportional to its weight
AliasTable(const std::vector< double > &w)
the table of the weights , not all zero
constexpr W operator()(void)
the next number, the next word of the buffer of two blocks, which is filled again when it is empty
static constexpr std::uint32_t default_seed
Definition RandomGenerators.h:385
constexpr void discard(unsigned long long z)
advances by outputs, in blocks
static constexpr Block block(Block c, Key k)
the block of the counter under the key : 10 rounds of the products of bits by the multipliers,...
W result_type
Definition RandomGenerators.h:380
static constexpr W min(void)
the least number, 0
Definition RandomGenerators.h:436
bool operator==(const PhiloxT &) const =default
true if the two states are equal
constexpr PhiloxT(W seed=default_seed)
the key and the counter 0
std::array< W, 4 > Block
< the type of the numbers
Definition RandomGenerators.h:381
static constexpr W max(void)
the largest number,
Definition RandomGenerators.h:438
constexpr void setCounter(const Block &c)
the counter of the next output, which will be the first word of its block
std::array< W, 2 > Key
< a counter, or its 4 outputs
Definition RandomGenerators.h:382
static constexpr std::uint64_t max(void)
the largest number,
Definition RandomGenerators.h:354
bool operator==(const WyRand &) const =default
true if the two states are equal
constexpr WyRand(std::uint64_t seed=1)
< the type of the numbers
Definition RandomGenerators.h:334
static constexpr std::uint64_t min(void)
the least number, 0
Definition RandomGenerators.h:352
std::uint64_t result_type
Definition RandomGenerators.h:331
constexpr std::uint64_t operator()(void)
the next number
xoshiro256++ (or xoshiro256** if StarStar), a std::uniform_random_bit_generator of 64 bits
Definition RandomGenerators.h:225
constexpr void jump(void)
advances by steps, a stream of numbers apart
bool operator==(const Xoshiro256T &) const =default
true if the two states are equal
static constexpr std::uint64_t max(void)
the largest number,
Definition RandomGenerators.h:271
static constexpr std::uint64_t min(void)
the least number, 0
Definition RandomGenerators.h:269
std::uint64_t result_type
Definition RandomGenerators.h:227
constexpr Xoshiro256T(std::uint64_t seed=1)
< the type of the numbers
constexpr std::uint64_t operator()(void)
the next number
a std::uniform_random_bit_generator of 64 full bits, from 0 to
Definition RandomGenerators.h:208
double uniformReal(G &&g)
a double uniform in , the top 53 bits of a word times
void shuffleFast(I first, I last, G &&g)
the items of shuffled by , every permutation equally likely, the indices drawn in batches
double exponentialZiggurat(G &&g)
an exponential deviate of mean 1, by the ziggurat of Marsaglia and Tsang with 256 layers
std::uint64_t uniformBelow(G &&g, std::uint64_t s)
an integer uniform in , by the method of Lemire
double normalZiggurat(G &&g)
a normal deviate of mean 0 and variance 1, by the ziggurat of Marsaglia and Tsang with 256 layers
#define ADS_RNG_INLINE
a function inlined where it is used, so that the state of the generator stays in the registers of the...
Definition RandomGenerators.h:200
Definition ADT.h:96
PhiloxT< std::uint64_t > Philox4x64
< Philox4x32-10
Definition RandomGenerators.h:458
Xoshiro256T< false > Xoshiro256
Definition RandomGenerators.h:320
Xoshiro256T< true > Xoshiro256StarStar
< xoshiro256++
Definition RandomGenerators.h:321
PhiloxT< std::uint32_t > Philox4x32
Definition RandomGenerators.h:457