2014 Eurobot fork
Dependencies: mbed-rtos mbed QEI
tvmet/util/Random.h@92:4a1225fbb146, 2013-10-15 (annotated)
- Committer:
- rsavitski
- Date:
- Tue Oct 15 12:19:32 2013 +0000
- Revision:
- 92:4a1225fbb146
- Parent:
- 15:9c5aaeda36dc
touch: ripped out 2013-specific bits. Need to address "2014" comments. Rewrite AI layer and other deleted parts.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 15:9c5aaeda36dc | 1 | /* |
madcowswe | 15:9c5aaeda36dc | 2 | * Tiny Vector Matrix Library |
madcowswe | 15:9c5aaeda36dc | 3 | * Dense Vector Matrix Libary of Tiny size using Expression Templates |
madcowswe | 15:9c5aaeda36dc | 4 | * |
madcowswe | 15:9c5aaeda36dc | 5 | * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> |
madcowswe | 15:9c5aaeda36dc | 6 | * |
madcowswe | 15:9c5aaeda36dc | 7 | * This library is free software; you can redistribute it and/or |
madcowswe | 15:9c5aaeda36dc | 8 | * modify it under the terms of the GNU Lesser General Public |
madcowswe | 15:9c5aaeda36dc | 9 | * License as published by the Free Software Foundation; either |
madcowswe | 15:9c5aaeda36dc | 10 | * version 2.1 of the License, or (at your option) any later version. |
madcowswe | 15:9c5aaeda36dc | 11 | * |
madcowswe | 15:9c5aaeda36dc | 12 | * This library is distributed in the hope that it will be useful, |
madcowswe | 15:9c5aaeda36dc | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
madcowswe | 15:9c5aaeda36dc | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
madcowswe | 15:9c5aaeda36dc | 15 | * Lesser General Public License for more details. |
madcowswe | 15:9c5aaeda36dc | 16 | * |
madcowswe | 15:9c5aaeda36dc | 17 | * You should have received a copy of the GNU Lesser General Public |
madcowswe | 15:9c5aaeda36dc | 18 | * License along with this library; if not, write to the Free Software |
madcowswe | 15:9c5aaeda36dc | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
madcowswe | 15:9c5aaeda36dc | 20 | * |
madcowswe | 15:9c5aaeda36dc | 21 | * $Id: Random.h,v 1.7 2007-06-23 15:58:59 opetzold Exp $ |
madcowswe | 15:9c5aaeda36dc | 22 | */ |
madcowswe | 15:9c5aaeda36dc | 23 | |
madcowswe | 15:9c5aaeda36dc | 24 | #ifndef TVMET_UTIL_RANDOM_H |
madcowswe | 15:9c5aaeda36dc | 25 | #define TVMET_UTIL_RANDOM_H |
madcowswe | 15:9c5aaeda36dc | 26 | |
madcowswe | 15:9c5aaeda36dc | 27 | #include <tvmet/CompileTimeError.h> |
madcowswe | 15:9c5aaeda36dc | 28 | |
madcowswe | 15:9c5aaeda36dc | 29 | namespace tvmet { |
madcowswe | 15:9c5aaeda36dc | 30 | |
madcowswe | 15:9c5aaeda36dc | 31 | namespace util { |
madcowswe | 15:9c5aaeda36dc | 32 | |
madcowswe | 15:9c5aaeda36dc | 33 | |
madcowswe | 15:9c5aaeda36dc | 34 | /** |
madcowswe | 15:9c5aaeda36dc | 35 | * \class Random Random.h "tvmet/util/Random.h" |
madcowswe | 15:9c5aaeda36dc | 36 | * \brief A simple random class. |
madcowswe | 15:9c5aaeda36dc | 37 | * On each access this class returns a new random number using |
madcowswe | 15:9c5aaeda36dc | 38 | * std::rand(). The range generated is templated by MIN and |
madcowswe | 15:9c5aaeda36dc | 39 | * MAX. |
madcowswe | 15:9c5aaeda36dc | 40 | * \ingroup _util_function |
madcowswe | 15:9c5aaeda36dc | 41 | * |
madcowswe | 15:9c5aaeda36dc | 42 | * \par Example: |
madcowswe | 15:9c5aaeda36dc | 43 | * \code |
madcowswe | 15:9c5aaeda36dc | 44 | * #include <algorithm> |
madcowswe | 15:9c5aaeda36dc | 45 | * |
madcowswe | 15:9c5aaeda36dc | 46 | * tvmet::Random<int, 0, 100> random; |
madcowswe | 15:9c5aaeda36dc | 47 | * |
madcowswe | 15:9c5aaeda36dc | 48 | * std::generate(m1.begin(), m1.end(), random()); |
madcowswe | 15:9c5aaeda36dc | 49 | * \endcode |
madcowswe | 15:9c5aaeda36dc | 50 | */ |
madcowswe | 15:9c5aaeda36dc | 51 | template<class T, int MIN=0, int MAX=100> |
madcowswe | 15:9c5aaeda36dc | 52 | class Random { |
madcowswe | 15:9c5aaeda36dc | 53 | static unsigned int s_seed; |
madcowswe | 15:9c5aaeda36dc | 54 | public: |
madcowswe | 15:9c5aaeda36dc | 55 | typedef T value_type; |
madcowswe | 15:9c5aaeda36dc | 56 | Random() { TVMET_CT_CONDITION(MIN<MAX, wrong_random_range) } |
madcowswe | 15:9c5aaeda36dc | 57 | value_type operator()() { |
madcowswe | 15:9c5aaeda36dc | 58 | s_seed += (unsigned)std::time(0); |
madcowswe | 15:9c5aaeda36dc | 59 | std::srand(s_seed); |
madcowswe | 15:9c5aaeda36dc | 60 | return MIN + int(double(MAX) * std::rand()/(double(RAND_MAX)+1.0)); |
madcowswe | 15:9c5aaeda36dc | 61 | } |
madcowswe | 15:9c5aaeda36dc | 62 | }; |
madcowswe | 15:9c5aaeda36dc | 63 | // instance |
madcowswe | 15:9c5aaeda36dc | 64 | template<class T, int MIN, int MAX> |
madcowswe | 15:9c5aaeda36dc | 65 | unsigned int Random<T, MIN, MAX>::s_seed; |
madcowswe | 15:9c5aaeda36dc | 66 | |
madcowswe | 15:9c5aaeda36dc | 67 | |
madcowswe | 15:9c5aaeda36dc | 68 | #if defined(TVMET_HAVE_COMPLEX) |
madcowswe | 15:9c5aaeda36dc | 69 | /** |
madcowswe | 15:9c5aaeda36dc | 70 | * \class Random< std::complex<T> > Random.h "tvmet/util/Random.h" |
madcowswe | 15:9c5aaeda36dc | 71 | * \brief Specialized Random class. |
madcowswe | 15:9c5aaeda36dc | 72 | * \ingroup _util_function |
madcowswe | 15:9c5aaeda36dc | 73 | */ |
madcowswe | 15:9c5aaeda36dc | 74 | template<class T, int MIN=0, int MAX=100> |
madcowswe | 15:9c5aaeda36dc | 75 | class Random { |
madcowswe | 15:9c5aaeda36dc | 76 | static unsigned int s_seed; |
madcowswe | 15:9c5aaeda36dc | 77 | public: |
madcowswe | 15:9c5aaeda36dc | 78 | typedef std::complex<T> value_type; |
madcowswe | 15:9c5aaeda36dc | 79 | Random() { TVMET_CT_CONDITION(MIN<MAX, wrong_random_range) } |
madcowswe | 15:9c5aaeda36dc | 80 | value_type operator()() { |
madcowswe | 15:9c5aaeda36dc | 81 | s_seed += (unsigned)std::time(0); |
madcowswe | 15:9c5aaeda36dc | 82 | std::srand(s_seed); |
madcowswe | 15:9c5aaeda36dc | 83 | return MIN + int(double(MAX) * std::rand()/(double(RAND_MAX)+1.0)); |
madcowswe | 15:9c5aaeda36dc | 84 | } |
madcowswe | 15:9c5aaeda36dc | 85 | }; |
madcowswe | 15:9c5aaeda36dc | 86 | // instance |
madcowswe | 15:9c5aaeda36dc | 87 | template<class T, int MIN, int MAX> |
madcowswe | 15:9c5aaeda36dc | 88 | unsigned int Random<std::complex<T>, MIN, MAX>::s_seed; |
madcowswe | 15:9c5aaeda36dc | 89 | #endif // defined(TVMET_HAVE_COMPLEX) |
madcowswe | 15:9c5aaeda36dc | 90 | |
madcowswe | 15:9c5aaeda36dc | 91 | |
madcowswe | 15:9c5aaeda36dc | 92 | } // namespace util |
madcowswe | 15:9c5aaeda36dc | 93 | |
madcowswe | 15:9c5aaeda36dc | 94 | } // namespace tvmet |
madcowswe | 15:9c5aaeda36dc | 95 | |
madcowswe | 15:9c5aaeda36dc | 96 | #endif // TVMET_UTIL_RANDOM_H |
madcowswe | 15:9c5aaeda36dc | 97 | |
madcowswe | 15:9c5aaeda36dc | 98 | // Local Variables: |
madcowswe | 15:9c5aaeda36dc | 99 | // mode:C++ |
madcowswe | 15:9c5aaeda36dc | 100 | // tab-width:8 |
madcowswe | 15:9c5aaeda36dc | 101 | // End: |