CMS 3D CMS Logo

SprIntegerPermutator.cc

Go to the documentation of this file.
00001 //$Id: SprIntegerPermutator.cc,v 1.2 2007/09/21 22:32:10 narsky Exp $
00002 
00003 #include "PhysicsTools/StatPatternRecognition/interface/SprExperiment.hh"
00004 #include "PhysicsTools/StatPatternRecognition/interface/SprIntegerPermutator.hh"
00005 
00006 #include <algorithm>
00007 #include <cassert>
00008 #include <iostream>
00009 
00010 using namespace std;
00011 
00012 
00013 SprIntegerPermutator::SprIntegerPermutator(unsigned N, int seed) 
00014   : 
00015   n_(N), 
00016   generator_(seed) 
00017 {
00018   for( unsigned i=0;i<N;i++ ) n_[i] = i;
00019 }
00020 
00021 
00022 bool SprIntegerPermutator::sequence(std::vector<unsigned>& seq)
00023 {
00024   // init
00025   const unsigned N = n_.size();
00026   seq = n_;
00027 
00028   // generate random numbers
00029   double* r = new double [N];
00030   generator_.sequence(r,N);
00031 
00032   // make a permutation
00033   for( unsigned i=0;i<N;i++ ) {
00034     unsigned j = i + unsigned((N-i)*r[i]);
00035     assert( j>=i && j<N );
00036     swap(seq[i],seq[j]);
00037   }
00038 
00039   // cleanup
00040   delete [] r;
00041 
00042   // exit
00043   return true;
00044 }

Generated on Tue Jun 9 17:42:03 2009 for CMSSW by  doxygen 1.5.4