Alex Bikfalvi
SimStream Documentation
ModelChannelFlat2Cat.cpp
00001 #include "Headers.h" 00002 #include "ModelChannelFlat2Cat.h" 00003 #include "Rand.h" 00004 00005 CModelChannelFlat2Cat::CModelChannelFlat2Cat( 00006 __uint32 numChannels, 00007 __uint32 numPopular, 00008 double probPopular 00009 ) : CModelChannelData(numChannels) 00010 { 00011 this->numPopular = numPopular; 00012 this->probPopular = (__uint32)(0xFFFFFFFF * probPopular); 00013 00014 } 00015 00016 void CModelChannelFlat2Cat::GenerateUptime(__uint32 &newCh, __time &duration) 00017 { 00018 __uint32 pr = CRand::GenerateUInt32(); 00019 __uint32 ch = CRand::GenerateUInt32(); 00020 __uint32 dindex; 00021 00022 //#if defined(DETRAND) && defined(_WINDOWS) 00023 // rand_s(&pr); 00024 // rand_s(&ch); 00025 //#else 00026 //#if RAND_MAX == 0x7FFF 00027 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00028 // ch = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00029 //#else 00030 // pr = (((__uint32)rand()) < 1) | (((__uint32)rand()) & 0x1); 00031 // ch = (((__uint32)rand()) < 1) | (((__uint32)rand()) & 0x1); 00032 //#endif 00033 //#endif 00034 00035 dindex = channelCdfIndex[pr >> 15]; 00036 duration = channelCdf[dindex][0] + CRand::Generate(); 00037 00038 pr = CRand::GenerateUInt32(); 00039 //#if defined(DETRAND) && defined(_WINDOWS) 00040 // rand_s(&pr); 00041 //#else 00042 //#if RAND_MAX == 0x7FFF 00043 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00044 //#else 00045 // pr = (((__uint32)rand()) < 1) | (((__uint32)rand()) & 0x1); 00046 //#endif 00047 //#endif 00048 00049 // Generate new channel (a random channel) 00050 if(pr < this->probPopular) 00051 newCh = (__uint32)(((__uint64)ch)*this->numPopular/0x100000000LL); 00052 else 00053 newCh = this->numPopular + (__uint32)(((__uint64)ch)*(this->numChannels-this->numPopular)/0x100000000LL); 00054 00055 assert(newCh < this->numChannels); 00056 } 00057 00058 void CModelChannelFlat2Cat::GenerateUptime(__uint32 oldCh, __time oldDuration, __uint32 &newCh, __time &duration) 00059 { 00060 __uint32 pr = CRand::GenerateUInt32(); 00061 __uint32 prb = CRand::GenerateUInt32(); 00062 __uint32 ch = CRand::GenerateUInt32(); 00063 __uint32 dindex; 00064 00065 //#ifndef DETRAND 00066 // rand_s(&pr); 00067 // rand_s(&ch); 00068 // rand_s(&prb); 00069 //#else 00070 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00071 // ch = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00072 // prb = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00073 //#endif 00074 00075 dindex = channelCdfIndex[pr >> 15]; 00076 duration = channelCdf[dindex][0] + CRand::Generate(); 00077 00078 pr = CRand::GenerateUInt32(); 00079 //#ifndef DETRAND 00080 // rand_s(&pr); 00081 //#else 00082 // pr = (((__uint32)rand()) << 17) | (((__uint32)rand()) << 2) | (((__uint32)rand()) & 0x3); 00083 //#endif 00084 00085 // Switch 00086 if(pr < this->probPopular) 00087 { 00088 // Switch to popular channel 00089 if(oldCh < this->numPopular) 00090 { 00091 newCh = (__uint32)(((__uint64)ch)*(this->numPopular-1)/0x100000000LL); 00092 if(newCh >= oldCh) newCh++; 00093 } 00094 else 00095 newCh = (__uint32)(((__uint64)ch)*this->numPopular/0x100000000LL); 00096 } 00097 else 00098 { 00099 // Switch to unpopular channel 00100 if(oldCh < this->numPopular) 00101 newCh = this->numPopular + (__uint32)(((__uint64)ch)*(this->numChannels-this->numPopular)/0x100000000LL); 00102 else 00103 { 00104 newCh = this->numPopular + (__uint32)(((__uint64)ch)*(this->numChannels-this->numPopular-1)/0x100000000LL); 00105 if(newCh >= oldCh) newCh++; 00106 } 00107 } 00108 00109 assert(newCh != oldCh); 00110 assert(newCh < this->numChannels); 00111 }
Last updated: February 8, 2011