Random.cc

Go to the documentation of this file.
00001 #include <cstdlib>
00002 
00003 #include "../debug/DebugUtils.h"
00004 #include "Random.h"
00005 
00006 namespace oasys {
00007 
00008 ByteGenerator::ByteGenerator(unsigned int seed)
00009     : cur_(seed) 
00010 { 
00011     next(); 
00012 }
00013 
00014 void 
00015 ByteGenerator::fill_bytes(void* buf, size_t size)
00016 {
00017     char* p = static_cast<char*>(buf);
00018     
00019     for(size_t i=0; i<size; ++i) {
00020         *p = cur_ % 256;
00021         ++p;
00022         next();
00023     }
00024 }
00025 
00026 void ByteGenerator::next() 
00027 {
00028     cur_ = (A * cur_ + C) % M;
00029 }
00030 
00031 PermutationArray::PermutationArray(size_t size)
00032     : size_(size)
00033 {
00034     array_.reserve(size_);
00035     for(unsigned int i=0; i<size_; ++i) {
00036         array_[i] = i;
00037     }
00038 
00039     for(unsigned int i=0; i<size_ - 1; ++i) {
00040         size_t temp;
00041         unsigned int pos = Random::rand(size_-i-1)+i+1;
00042         
00043         temp = array_[i];
00044         array_[i]   = array_[pos];
00045         array_[pos] = temp;
00046     }
00047 }
00048 
00049 unsigned int
00050 PermutationArray::map(unsigned int i) {
00051     ASSERT(i<size_); 
00052     return array_[i];
00053 }
00054 
00055 }; // namespace oasys
00056 
00057 #if 0
00058 #include <iostream>
00059 
00060 using namespace std;
00061 using namespace oasys;
00062 
00063 int
00064 main()
00065 {
00066     const size_t SIZE = 20;
00067     PermutationArray ar(SIZE);
00068 
00069     for(int i=0;i<SIZE;++i) {
00070         cout << i << "->" << ar.map(i) << endl;
00071     }
00072 }
00073 #endif

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1