MRPT  2.0.0
random_shuffle.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <iterator> // iterator_traits
13 #include <random> // uniform_int_distribution
14 #include <utility> // std::swap
15 
16 namespace mrpt
17 {
18 namespace random
19 {
20 /** Uniform shuffle a sequence.
21  *\ingroup mrpt_random_grp
22  */
23 template <class RandomIt, class URBG>
24 void shuffle(RandomIt first, RandomIt last, URBG&& g)
25 {
26  typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
27  typedef std::uniform_int_distribution<diff_t> distr_t;
28  typedef typename distr_t::param_type param_t;
29  distr_t D;
30  diff_t n = last - first;
31  for (diff_t i = n - 1; i > 0; --i)
32  std::swap(first[i], first[D(g, param_t(0, i))]);
33 }
34 
35 /** Uniform shuffle a sequence.
36  *\ingroup mrpt_random_grp
37  */
38 template <class RandomIt>
39 void shuffle(RandomIt first, RandomIt last)
40 {
41  std::random_device rd; // used for random seed
42  std::mt19937 g(rd());
43  mrpt::random::shuffle(first, last, g);
44 }
45 
46 } // namespace random
47 } // namespace mrpt
void shuffle(RandomIt first, RandomIt last, URBG &&g)
Uniform shuffle a sequence.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020