Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
random_permutation.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/random/random_permutation.hh
11 /// @brief Reorder the elements in a vector using a RNG object
12 /// @author Andrew Leaver-Fay
13 ///
14 /// @remarks
15 /// @li -
16 
17 
18 #ifndef INCLUDED_numeric_random_random_permutation_hh
19 #define INCLUDED_numeric_random_random_permutation_hh
20 
21 
22 // Unit headers
23 #include <numeric/random/random.hh>
24 
25 // Utility headers
26 #include <utility/vector0.hh>
27 #include <utility/vector1.hh>
28 
29 // C++ Headers
30 #include <algorithm>
31 #include <cmath>
32 #include <iostream>
33 
34 namespace numeric {
35 namespace random {
36 
37 template< class T >
38 void
40  utility::vector1< T > & vect,
42 )
43 {
44  Size const vsize = vect.size();
45  for ( Size ii = vsize; ii >= 2; --ii ) {
46  Size swap_partner = static_cast< Size > (std::floor( ii*rg.uniform() + 1 ));
47 
48  T temp = vect[ swap_partner ];
49  vect[ swap_partner ] = vect[ ii ];
50  vect[ ii ] = temp;
51  }
52 }
53 
54 template< class T >
55 void
57  utility::vector0< T > & vect,
59 )
60 {
61  Size const vsize = vect.size();
62  for ( Size ii = vsize - 1; ii >= 1; --ii ) {
63  Size swap_partner = static_cast< Size > ( std::floor( ii*rg.uniform() ));
64 
65  T temp = vect[ swap_partner ];
66  vect[ swap_partner ] = vect[ ii ];
67  vect[ ii ] = temp;
68  }
69 }
70 
71 
72 template< class T >
73 void
75  std::vector< T > & vect,
77 )
78 {
79  Size const vsize = vect.size();
80  for ( Size ii = vsize - 1; ii >= 1; --ii ) {
81  Size swap_partner = static_cast< Size > ( std::floor( ii*rg.uniform() ));
82 
83  T temp = vect[ swap_partner ];
84  vect[ swap_partner ] = vect[ ii ];
85  vect[ ii ] = temp;
86  }
87 }
88 
89 
90 /// @brief randomly shuffle elements of a sequence
91 template< typename RandomAccessIterator >
92 void
94  RandomAccessIterator first,
95  RandomAccessIterator last,
97 )
98 {
99  if ( first != last ) {
100  for ( RandomAccessIterator i = first + 1; i != last; ++i ) {
101  std::iter_swap( i, first + static_cast< Size >( ( i - first + 1 ) * rg.uniform() ) );
102  }
103  }
104 }
105 
106 
107 } // namespace random
108 } // namespace numeric
109 
110 
111 #endif // INCLUDED_numeric_random_random_permutation_HH
RandomGenerator & rg()
Return the one-per-thread "singleton" random generator.
Definition: random.cc:46
vector0: std::vector with assert-checked bounds
void random_permutation(utility::vector1< T > &vect, RandomGenerator &rg)
Random number generator system.
Definition: random.hh:73
Random number generator system.
vector1: std::vector with 1-based indexing
platform::Size Size
Definition: random.fwd.hh:30