首页
社区
课程
招聘
[分享]一个随机数生成类
发表于: 2009-1-9 14:53 4479

[分享]一个随机数生成类

2009-1-9 14:53
4479
/* RandomInt.h declares RandomInt, pseudo-random integer class.
 *
 * Written by: Joel C. Adams, Spring, 1993, at Calvin College.
 * Written for: C++: An Introduction To Computing.
 * Revised: Spring 1997, for C++: An Introduction To Computing 2e.
 * Revised: Summer 2001, for C++: An Introduction To Computing 3e.
 *
 * See RandomInt.doc for class documentation,
 *     RandomInt.cpp for non-trivial operation definitions.
 ******************************************************************/

#ifndef RANDOMINT
#define RANDOMINT

#include <iostream>
#include <cstdlib>
#include <cassert>
#include <ctime>
using namespace std;

class RandomInt
{
 public:
  RandomInt();
  RandomInt(int low, int high);
  RandomInt(int seedValue);
  RandomInt(int seedValue, int low, int high);
  
  void print(ostream & out) const;
  RandomInt generate();
  RandomInt generate(int low, int high);
  operator int();

 private:
  void initialize(int low, int high);
  void seededInitialize(int seedValue, int low, int high);
  int nextRandomInt();

  int myLowerBound,
      myUpperBound,
      myRandomValue;

  static bool generatorInitialized;
};

inline RandomInt::RandomInt()
{
  initialize(0, RAND_MAX);
}

inline RandomInt::RandomInt(int low, int high)
{
  assert(0 <= low);
  assert(low < high);
  initialize(low, high);
}

inline RandomInt::RandomInt(int seedValue)
{
  assert(seedValue >= 0);
  seededInitialize(seedValue, 0, RAND_MAX);
}

inline RandomInt::RandomInt(int seedValue, int low, int high)
{
  assert(seedValue >= 0);
  assert(0 <= low);
  assert(low < high);
  seededInitialize(seedValue, low, high);
}

inline void RandomInt::print(ostream & out) const
{
  out << myRandomValue;
}

inline ostream & operator<< (ostream & out, const RandomInt & randInt)
{
  randInt.print(out);
  return out;
}

inline int RandomInt::nextRandomInt()
{
  return myLowerBound + rand() % (myUpperBound - myLowerBound + 1);
}

inline RandomInt RandomInt::generate()
{
  myRandomValue = nextRandomInt();
  return *this;
}

inline RandomInt::operator int() 
{
  return myRandomValue;
}

#endif


/* RandomInt.cpp defines the non-trivial RandomInt operations.

 * Written by: Joel C. Adams, Spring, 1993, at Calvin College.
 * Written for: C++: An Introduction To Computing.
 * Revised: Spring 1997, for C++: An Introduction To Computing 2e.
 * Revised: Summer 2001, for C++: An Introduction To Computing 3e.
 *
 * See RandomInt.h for the class declaration,
 *     RandomInt.doc for class documentation.
 ******************************************************************/

#include "RandomInt.h"

bool RandomInt::generatorInitialized = false;

void RandomInt::initialize(int low, int high)
{
  myLowerBound = low;
  myUpperBound = high;

  if (!generatorInitialized)       // call srand() first time only
  {
     srand(long(time(0)));        // use clock for seed
     generatorInitialized = true;
  }

  myRandomValue = nextRandomInt();
}


void RandomInt::seededInitialize(int seedValue, int low, int high)
{
  myLowerBound = low;
  myUpperBound = high;
  srand(seedValue);
  generatorInitialized = true;
  myRandomValue = nextRandomInt();
}


RandomInt RandomInt::generate(int low, int high)
{
  assert(0 <= low);
  assert(low < high);
  myLowerBound = low;
  myUpperBound = high;
  myRandomValue = nextRandomInt();
  return *this;
}

[培训]科锐逆向工程师培训第53期2025年7月8日开班!

上传的附件:
收藏
免费 0
支持
分享
最新回复 (4)
雪    币: 5325
活跃值: (5360)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
谢谢分享...
2009-1-9 15:37
0
雪    币: 709
活跃值: (2590)
能力值: ( LV12,RANK:1010 )
在线值:
发帖
回帖
粉丝
3
驱动中偶就用 TickCounts srand 来随机名.呵呵
2009-1-9 19:39
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
946K9s2c8@1M7q4)9K6b7g2)9J5c8W2)9J5c8Y4N6%4N6#2)9J5k6h3q4Y4L8X3g2J5i4K6u0W2L8%4u0Y4i4K6u0r3M7X3q4F1k6r3!0E0i4K6u0r3i4K6y4r3k6g2)9K6c8o6m8Q4x3U0x3H3
这上面有一些比较好的随机数生成库,可以学习下
2009-1-9 20:19
0
雪    币: 251
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
谢了,下载学习了.
2009-1-10 12:01
0
游客
登录 | 注册 方可回帖
返回