Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef RANDOM_INTERVAL_GENERATOR_H |
| 23 | #define RANDOM_INTERVAL_GENERATOR_H |
| 24 | |
| 25 | #include "interval-generator.h" |
| 26 | |
| 27 | #include <boost/random/mersenne_twister.hpp> |
| 28 | #include <boost/random/uniform_real.hpp> |
| 29 | #include <boost/random/variate_generator.hpp> |
| 30 | |
| 31 | // generates intervals with uniform distribution |
| 32 | class RandomIntervalGenerator : public IntervalGenerator |
| 33 | { |
| 34 | public: |
| 35 | typedef enum |
| 36 | { |
| 37 | UP = 1, |
| 38 | DOWN = 2, |
| 39 | EVEN = 3 |
| 40 | } Direction; |
| 41 | |
| 42 | public: |
| 43 | // percent is random-range/interval; e.g. if interval is 10 and you wish the random-range to be 2 |
| 44 | // e.g. 9 ~ 11, percent = 0.2 |
| 45 | // direction shifts the random range; e.g. in the above example, UP would produce a range of |
| 46 | // 10 ~ 12, DOWN of 8 ~ 10, and EVEN of 9 ~ 11 |
| 47 | RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN); |
| 48 | ~RandomIntervalGenerator(){} |
| 49 | |
| 50 | virtual double |
| 51 | nextInterval() _OVERRIDE; |
| 52 | |
| 53 | private: |
| 54 | inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); } |
| 55 | |
| 56 | private: |
| 57 | typedef boost::mt19937 RNG_TYPE; |
| 58 | RNG_TYPE m_rng; |
| 59 | boost::uniform_real<> m_dist; |
| 60 | boost::variate_generator<RNG_TYPE &, boost::uniform_real<> > m_random; |
| 61 | Direction m_direction; |
| 62 | double m_percent; |
| 63 | double m_interval; |
| 64 | |
| 65 | }; |
| 66 | #endif // RANDOM_INTERVAL_GENERATOR_H |