libcruft-util/random.cpp

60 lines
1.5 KiB
C++
Raw Normal View History

2011-09-13 15:13:55 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-09-13 15:13:55 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-09-13 15:13:55 +10:00
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2011-09-13 15:13:55 +10:00
*
2012-04-23 13:06:41 +10:00
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
2011-09-13 15:13:55 +10:00
*/
#include "random.hpp"
#include "range.hpp"
2012-05-22 14:13:07 +10:00
#include <type_traits>
2011-09-13 15:13:55 +10:00
using namespace util;
namespace util {
template <typename T>
T
2012-05-22 14:13:07 +10:00
random (void) {
static_assert (std::is_integral<T>::value, "random should only operate on integral types");
return range<T>::UNLIMITED.random ();
}
template <>
double
random (void)
{ return range<double>::UNIT.random (); }
template <>
float
2011-09-13 15:13:55 +10:00
random (void)
2012-05-22 14:13:07 +10:00
{ return range<float>::UNIT.random (); }
2011-09-13 15:13:55 +10:00
2013-07-30 14:28:22 +10:00
template <>
bool
random (void)
{ return rand () & 0x01; }
2011-09-13 15:13:55 +10:00
template <typename T>
T&
randomise (T &val)
{ return val = util::random<T> (); }
}
template double util::random (void);
template float util::random (void);
template uint64_t util::random (void);
2012-01-04 17:03:49 +11:00
template uint32_t util::random (void);
template uint16_t util::random (void);