SimpleNonRepeatRandomNG.cpp

Go to the documentation of this file.
00001 #include "SimpleNonRepeatRandomNG.h"
00002 #include "RandomEngine.h"
00003 #include "PCSIMException.h"
00004 
00005 #include <algorithm>
00006 #include <limits>
00007 
00008 using std::swap;
00009 
00010 
00011 SimpleNonRepeatRandomNG::SimpleNonRepeatRandomNG()
00012    : seq_array(NULL), cursor(0), unidist(0, 1) 
00013 {
00014         
00015 }
00016 
00017 SimpleNonRepeatRandomNG::SimpleNonRepeatRandomNG(int low, int high)
00018    : low(low), high(high), cursor(0), unidist(0, 1)
00019 {
00020         range = high - low + 1;
00021     seq_array = new int[high-low+1];
00022     for (int i = 0; i < range; ++i)
00023         seq_array[i] = i;
00024 }
00025 
00026 SimpleNonRepeatRandomNG::~SimpleNonRepeatRandomNG()
00027 {
00028         if (seq_array) 
00029         delete [] seq_array;
00030 }
00031 
00032 int SimpleNonRepeatRandomNG::operator()( RandomEngine &eng )
00033 {
00034     if (cursor > num_elem) {
00035         throw PCSIM::Exception("SimpleNonRepeatRandomNG::operator()", "ERROR: Tried to generate more random numbers than num_elem");
00036     }
00037     int n = int( unidist( eng ) * (range - cursor) );
00038     // n = n % (range - cursor);
00039     int rand_num = seq_array[cursor + n];
00040     swap(seq_array[cursor], seq_array[cursor + n]);
00041     generated_numbers.push_back(n);
00042     cursor++;
00043     return low + rand_num;
00044 }

Generated on Wed Jul 9 16:34:39 2008 for PCSIM by  doxygen 1.5.5