/* * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. * * Copyright 2010 Danny Robson */ #ifndef __UTIL_RANDOM_HPP #define __UTIL_RANDOM_HPP #include namespace util { template T& randomise (T &); template T* randomise (T(&)[N]); template T random (void); template typename T::value_type& choose (T &container) { typename T::iterator cursor = container.begin (); typename T::size_type size = container.size (); typename T::size_type offset = random () % size; std::advance (cursor, offset); return *cursor; } template T& choose (T (&v)[N]) { return v[static_cast (random () * N)]; } template typename T::value_type& choose (T begin, T end) { typename T::difference_type size = std::distance (begin, end); std::advance (begin, random () % size); return *begin; } } #include "random.ipp" #endif