Tapkee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
random.hpp
Go to the documentation of this file.
1 /* This software is distributed under BSD 3-clause license (see LICENSE file).
2  *
3  * Copyright (c) 2012-2013 Sergey Lisitsyn
4  */
5 
6 #ifndef TAPKEE_DEFINES_RANDOM_H_
7 #define TAPKEE_DEFINES_RANDOM_H_
8 
9 #include <cstdlib>
10 #include <algorithm>
11 #include <limits>
12 
13 namespace tapkee
14 {
15 
17 {
18 #ifdef CUSTOM_UNIFORM_RANDOM_INDEX_FUNCTION
19  return CUSTOM_UNIFORM_RANDOM_INDEX_FUNCTION % std::numeric_limits<IndexType>::max();
20 #else
21  return std::rand();
22 #endif
23 }
24 
26 {
27  return uniform_random_index() % upper;
28 }
29 
31 {
32 #ifdef CUSTOM_UNIFORM_RANDOM_FUNCTION
33  return CUSTOM_UNIFORM_RANDOM_FUNCTION;
34 #else
35  return std::rand()/((double)RAND_MAX+1);
36 #endif
37 }
38 
40 {
41 #ifdef CUSTOM_GAUSSIAN_RANDOM_FUNCTION
42  return CUSTOM_GAUSSIAN_RANDOM_FUNCTION;
43 #else
44  ScalarType x, y, radius;
45  do {
46  x = 2*(std::rand()/((double)RAND_MAX+1)) - 1;
47  y = 2*(std::rand()/((double)RAND_MAX+1)) - 1;
48  radius = (x * x) + (y * y);
49  } while ((radius >= 1.0) || (radius == 0.0));
50  radius = std::sqrt(-2 * std::log(radius) / radius);
51  x *= radius;
52  y *= radius;
53  return x;
54 #endif
55 }
56 
57 template <class RAI>
58 inline void random_shuffle(RAI first, RAI last)
59 {
61 }
62 
63 }
64 
65 #endif
66 
67 
void random_shuffle(RAI first, RAI last)
Definition: random.hpp:58
ScalarType uniform_random()
Definition: random.hpp:30
double ScalarType
default scalar value (can be overrided with TAPKEE_CUSTOM_INTERNAL_NUMTYPE define) ...
Definition: types.hpp:15
int IndexType
indexing type (non-overridable) set to int for compatibility with OpenMP 2.0
Definition: types.hpp:19
ScalarType gaussian_random()
Definition: random.hpp:39
IndexType uniform_random_index_bounded(IndexType upper)
Definition: random.hpp:25
IndexType uniform_random_index()
Definition: random.hpp:16