00001
00002
00017 #ifndef TESTTOOLS_RANDOM_H
00018 #define TESTTOOLS_RANDOM_H
00019
00020
00021 #include <stdint.h>
00022
00023
00024 namespace Athena_test {
00025
00026
00028 static uint32_t rngmax = static_cast<uint32_t> (-1);
00029
00030
00032 uint32_t rng_seed (uint32_t& seed)
00033 {
00034 seed = (1664525*seed + 1013904223);
00035 return seed;
00036 }
00037
00038
00040 float randf_seed (uint32_t& seed, float rmax, float rmin = 0)
00041 {
00042 return static_cast<float>(rng_seed(seed)) / rngmax * (rmax-rmin) + rmin;
00043 }
00044
00045
00047 int randi_seed (uint32_t& seed, int rmax, int rmin = 0)
00048 {
00049 return static_cast<int> (randf_seed (seed, rmax, rmin));
00050 }
00051
00052
00054 struct RNG
00055 {
00056 RNG() : seed(1) {}
00057 int operator() (int n) const { return randi_seed (seed, n); }
00058 mutable uint32_t seed;
00059 };
00060
00061
00063 struct URNG
00064 {
00065 typedef uint32_t result_type;
00066 URNG() : seed(1) {}
00067 static result_type min() { return 0; }
00068 static result_type max() { return 1000000; }
00069 result_type operator()() const { return randi_seed (seed, max()); }
00070 mutable uint32_t seed;
00071 };
00072
00073
00074 uint32_t seed = 1;
00075 uint32_t rng() { return rng_seed(seed); }
00076 int randi (int rmax, int rmin = 0) { return randi_seed (seed, rmax, rmin); }
00077 float randf (float rmax, float rmin = 0) { return randf_seed (seed, rmax, rmin); }
00078
00079
00080
00081 }
00082
00083
00084 #endif // not TESTTOOLS_RANDOM_H