const int type = rand() % 3;switch(type){case 0: A(); break;case 1: B(); break;case 2: C(); break;default: assert(0); break;}
在VS2008 sp1平台下的输出结果为:#include <vector>#include <iostream>#include <cassert>#include <algorithm>using std::cout;using std::endl;void rand_test(int n, int times){
std::vector<int> data; data.resize(n); for(int i = 0; i < times; ++i){
++data[rand() % n]; }
for(size_t i = 0; i < data.size(); i++) {
cout << i << " - " << data[i] << "\t"; }
cout << "min - " << std::distance(data.begin(), std::min_element(da ta.begin(), da ta.end())); cout << endl;}
void main(){
assert(RAND_MAX == 0x7FFF);// 这里的0x7FFF是确保你所使用的rand函数最大值是32767(可以取到)
const int n = 3;rand_test(n, 0x000FFFFF);rand_test(n, 0x001FFFFF);rand_test(n, 0x002FFFFF);rand_test(n, 0x003FFFFF);rand_test(n, 0x004FFFFF);rand_test(n, 0x005FFFFF);rand_test(n, 0x006FFFFF);rand_test(n, 0x007FFFFF);rand_test(n, 0x008FFFFF);rand_test(n, 0x009FFFFF);rand_test(n, 0x00AFFFFF);rand_test(n, 0x00BFFFFF);rand_test(n, 0x00CFFFFF);rand_test(n, 0x00DFFFFF);rand_test(n, 0x00EFFFFF);rand_test(n, 0x00FFFFFF);}
0 - 349742 1 - 350201 2 - 348632 min - 20 - 699104 1 - 698089 2 - 699958 min - 10 - 1046530 1 - 1050468 2 - 1048729 min - 00 - 1398442 1 - 1399490 2 - 1396371 min - 20 - 1748016 1 - 1748998 2 - 1745865 min - 20 - 2097960 1 - 2097308 2 - 2096187 min - 20 - 2446782 1 - 2447505 2 - 2445744 min - 20 - 2797492 1 - 2796220 2 - 2794895 min - 20 - 3145117 1 - 3147283 2 - 3144783 min - 20 - 3494002 1 - 3496022 2 - 3495735 min - 00 - 3844733 1 - 3845287 2 - 3844315 min - 20 - 4194914 1 - 4194537 2 - 4193460 min - 20 - 4545303 1 - 4541730 2 - 4544454 min - 10 - 4892986 1 - 4894462 2 - 4892615 min - 20 - 5244394 1 - 5243212 2 - 5241033 min - 20 - 5594917 1 - 5591010 2 - 5591288 min - 1
20100112
ENT随机数质量测试算法:http://www.fourmilab.ch/random/
评论