Alex Bikfalvi
SimStream Documentation
Shuffle.cpp
00001 #include "Headers.h" 00002 #include "Shuffle.h" 00003 #include "Rand.h" 00004 00005 CShuffle::CShuffle( 00006 __uint32 count 00007 ) 00008 { 00009 assert(count); 00010 00011 this->count = count; 00012 00013 this->mapping = new __uint32[this->count]; 00014 assert(this->mapping); 00015 00016 for(__uint32 index = 0; index < this->count; index++) 00017 this->mapping[index] = index; 00018 00019 this->Shuffle(); 00020 } 00021 00022 CShuffle::~CShuffle() 00023 { 00024 delete[] this->mapping; 00025 } 00026 00027 void CShuffle::Shuffle() 00028 { 00029 /* 00030 * Portions of this code are based on: http://benpfaff.org/writings/clc/shuffle.html (last updated 04 Apr 2004 16:19) 00031 * Copyright � 2004 Ben Pfaff. 00032 */ 00033 00034 __uint32 i2; 00035 __uint32 tmp; 00036 00037 for(__uint32 i1 = 0; i1 < this->count - 1; i1++) 00038 { 00039 i2 = i1 + CRand::Generate(this->count - i1); 00040 assert(i2 < this->count); 00041 00042 tmp = this->mapping[i2]; 00043 this->mapping[i2] = this->mapping[i1]; 00044 this->mapping[i1] = tmp; 00045 } 00046 }
Last updated: February 8, 2011