contrib/mul/mbl/mbl_random_n_from_m.h
Go to the documentation of this file.
00001 #ifndef mbl_random_n_from_m_h_
00002 #define mbl_random_n_from_m_h_
00003 //:
00004 // \file
00005 // \brief Randomly select n from m integers without replacement
00006 // \author Tim Cootes
00007 
00008 #include <vcl_vector.h>
00009 #include <vnl/vnl_random.h>
00010 
00011 //: Class to generate arrays of n integers chosen without replacement from [0,m-1]
00012 class mbl_random_n_from_m
00013 {
00014  private:
00015   vnl_random mz_random_;
00016   vcl_vector<bool> used_;
00017  public:
00018 
00019   //: Dflt ctor
00020   mbl_random_n_from_m();
00021 
00022   //: Set seed of random number generator
00023   void reseed(long new_seed);
00024 
00025   //: Select n integers from range [0,m-1], without replacement.
00026   //  ie all different
00027   //  n is required to be <= m; otherwise, the function abort()s.
00028   //  But n might be 0, in which case an empty list is returned.
00029   // \retval chosen  Array [0,n-1] of chosen elements
00030   void choose_n_from_m(vcl_vector<unsigned>& chosen,
00031                        unsigned int n, unsigned int m);
00032 
00033   //: Select n integers from range [0,m-1], without replacement.
00034   //  ie all different
00035   //  n is required to be <= m; otherwise, the function abort()s.
00036   //  But n might be 0, in which case an empty list is returned.
00037   // \retval chosen  Array [0,n-1] of chosen elements
00038   // \retval not_chosen  Array[0,m-n-1] of not chosen elements
00039   void choose_n_from_m(vcl_vector<unsigned>& chosen,
00040                        vcl_vector<unsigned>& not_chosen,
00041                        unsigned int n, unsigned int m);
00042 
00043   //: Select n integers from range [0,m-1], without replacement.
00044   //  ie all different
00045   //  n is required to be <= m; otherwise, the function abort()s.
00046   //  But n might be 0, in which case an empty list is returned.
00047   // \retval chosen  Array [0,n-1] of chosen elements
00048   void choose_n_from_m(vcl_vector<int>& chosen,
00049                        unsigned int n, unsigned int m);
00050 
00051   //: Select n integers from range [0,m-1], without replacement.
00052   //  ie all different
00053   //  n is required to be <= m; otherwise, the function abort()s.
00054   //  But n might be 0, in which case an empty list is returned.
00055   // \retval chosen  Array [0,n-1] of chosen elements
00056   // \retval not_chosen  Array[0,m-n-1] of not chosen elements
00057   void choose_n_from_m(vcl_vector<int>& chosen,
00058                        vcl_vector<int>& not_chosen,
00059                        unsigned int n, unsigned int m);
00060 };
00061 
00062 #endif // mbl_random_n_from_m_h_