random_shuffle.hpp 712 B

1234567891011121314151617181920212223242526
  1. /* Copyright (C) 2016 Edward Diener
  2. *
  3. * Use, modification and distribution is subject to the
  4. * Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_POOL_TEST_RANDOM_SHUFFLE_HPP
  8. #define BOOST_POOL_TEST_RANDOM_SHUFFLE_HPP
  9. #include <cstdlib>
  10. #include <iterator>
  11. #include <algorithm>
  12. template< class RandomIt >
  13. void pool_test_random_shuffle( RandomIt first, RandomIt last )
  14. {
  15. typename std::iterator_traits<RandomIt>::difference_type i, n;
  16. n = last - first;
  17. for (i = n-1; i > 0; --i) {
  18. using std::swap;
  19. swap(first[i], first[std::rand() % (i+1)]);
  20. }
  21. }
  22. #endif // BOOST_POOL_TEST_RANDOM_SHUFFLE_HPP