// file:          ToolsetRandomC.hpp
// description:   Utility class for random numbers.
// author:        Karlheinz Hug
// creation date: 2012-06-21
// last update:   2012-07-02

#ifndef TOOLSET_RANDOM_C_HPP_
#define TOOLSET_RANDOM_C_HPP_

#include <cassert>
#include <cstdlib>

	template <typename T>
	class ToolsetRandom
		// Instantiation condition: T is a comparable type.
	{
		public:
			static T uniform (T min, T max);
				// precondition: min <= max
				// The sequence of T objects generated by calls of this function
				// is uniformly distributed in the interval [min, max].
				// Implementation restriction: For small ranges, max is not generated!
	}; // ToolsetArrayComparable

	template <typename T>
	T ToolsetRandom<T>::uniform (T min, T max)
	{
		assert(min <= max); // precondition
		T result = T((double(std::rand()) / RAND_MAX) * (max - min) + min);
			// Mathematically correct formula, but for small ranges, max is not generated!
		assert(min <= result); // postcondition
		assert(result <= max); // postcondition
		return result;
	}

#endif // TOOLSET_RANDOM_C_HPP_
