⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randgen.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <time.h>                // for time()#include <stdlib.h>              // for rand/srand#include "randgen.h"#include <cmath>int RandGen::ourInitialized = 0;void RandGen::SetSeed(int seed)// postcondition: system srand() used to initialize seed//                once per program (this is a static function)    {    if (0 == ourInitialized)    {   ourInitialized = 1;   // only call srand once		srand(seed);          // randomize
		rand(); rand(); rand();    }}RandGen::RandGen()// postcondition: system srand() used to initialize seed//                once per program     {    if (0 == ourInitialized)    {   ourInitialized = 1;          // only call srand once        srand(unsigned(time(0)));    // randomize
		rand(); rand(); rand();    }}int RandGen::RandInt(int max)// precondition: max > 0// postcondition: returns int in [0..max)     {      return int(RandReal() * max);}int RandGen::RandInt(int low, int max)// precondition: low <= max     // postcondition: returns int in [low..max]     {     return low + RandInt(max-low+1);}double RandGen::RandReal()// postcondition: returns double in [0..1)     {         return rand() / (double(RAND_MAX) + 1); }double RandGen::RandReal(double low, double high){    double width = fabs(high-low);    double thelow = low < high ? low : high;    return RandReal()*width + thelow;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -