trsl logo
common.hpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2011.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
8 #ifndef TRSL_COMMON_HPP
9 #define TRSL_COMMON_HPP
10 
11 #include <cstdlib>
12 #include <algorithm> //iter_swap
13 
19 #define TRSL_VERSION "0.2.2"
20 
27 #define TRSL_VERSION_NR 100020200
28 
29 namespace trsl {
30 
32  namespace detail {
33 
34  template<typename T>
35  class identity
36  {
37  public:
38  T operator() (const T& t)
39  {
40  return t;
41  }
42  };
43 
44  template<typename RandomAccessIterator, typename RandomNumberGenerator>
45  void partial_random_shuffle(RandomAccessIterator first,
46  RandomAccessIterator middle,
47  RandomAccessIterator last,
48  RandomNumberGenerator &rg)
49  {
50  if (first == middle)
51  return;
52 
53  for (RandomAccessIterator i = first; i != middle; ++i)
54  {
55  std::iter_swap(i, i + rg(last - i));
56  }
57  }
58 
59  }
60 
62  namespace rand_gen {
63 
68  inline unsigned int uniform_int(unsigned int n)
69  {
70 #ifdef TRSL_USE_BSD_BETTER_RANDOM_GENERATOR
71  return ::random()%n;
72 #else
73  return std::rand()%n;
74 #endif
75  }
76 
81  template<typename Real>
82  inline Real uniform_01()
83  {
84 #ifdef TRSL_USE_BSD_BETTER_RANDOM_GENERATOR
85  return (::random() / (RAND_MAX+Real(1.0)));
86 #else
87  return (std::rand() / (RAND_MAX+Real(1.0)));
88 #endif
89  }
90 
91  }
92 }
93 
94 #endif // include guard
trsl
Public namespace.
Definition: common.hpp:29
trsl::rand_gen::uniform_01
Real uniform_01()
Returns a float in [0,1[. Used internally.
Definition: common.hpp:82
trsl::rand_gen::uniform_int
unsigned int uniform_int(unsigned int n)
Returns an integer in [0,n[. Used internally.
Definition: common.hpp:68
trsl::detail::identity
Definition: common.hpp:35
© Copyright 2007-2011 Renaud Detry.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt.)
Revised Wed Jan 8 2020 14:43:31.