/* * Copyright (C) 2021 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Antkeeper source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Antkeeper source code. If not, see . */ #ifndef ANTKEEPER_MATH_RANDOM_HPP #define ANTKEEPER_MATH_RANDOM_HPP #include #include namespace math { /** * Generates a pseudo-random floating point number on `[start, end)` using std::rand(). * * @warning Don't forget to seed with std::srand() before using! * * @param start Start of the range (inclusive). * @param end End of the range (exclusive). * @return Pseudo-random floating point number. */ template T random(T start, T end); template inline T random(T start, T end) { static_assert(std::is_floating_point::value); constexpr T rand_max_inverse = T(1) / static_cast(RAND_MAX); return static_cast(std::rand()) * rand_max_inverse * (end - start) + start; } } // namespace math #endif // ANTKEEPER_MATH_RANDOM_HPP