C++编程思想 第2卷 第5章 深入理解模板 模板参数 无类型模板参数

随机数生成器使用了bitset来跟踪这些数
在随机数生成器下一次工作周期重新开始之前
所有在允许范围内的数都将无重复地按照随机顺序返回

//: C05:Urand.h {-bor}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Unique randomizer.
#ifndef URAND_H
#define URAND_H
#include <bitset>
#include <cstddef>
#include <cstdlib>
#include <ctime>
using std::size_t;
using std::bitset;

template<size_t UpperBound> class Urand {
  bitset<UpperBound> used;
public:
  Urand() { srand(time(0)); } // Randomize
  size_t operator()(); // The "generator" function
};

template<size_t UpperBound>
inline size_t Urand<UpperBound>::operator()() {
  if(used.count() == UpperBound)
    used.reset();  // Start over (clear bitset)
  size_t newval;
  while(used[newval = rand() % UpperBound])
    ; // Until unique value is found
  used[newval] = true;
  return newval;
}
#endif // URAND_H ///:~

Urand生成的数全是独一无二的
因为bitset used跟踪了随机空间所有可能产生的数
并且设置相应的状态位来记录每一个使用过的数

//: C05:UrandTest.cpp {-bor}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <iostream>
#include "Urand.h"
using namespace std;

int main() {
  Urand<10> u;
  for(int i = 0; i < 20; ++i)
    cout << u() << ' ';
  getchar();
} ///:~

输出
2 4 8 5 9 3 0 6 7 1 5 7 0 1 6 2 8 3 4 9

无类型模板参数在优化数值计算方面重要

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/82055420