我可以在这里使用全局变量而不是静态局部变量吗?

时间:2016-06-18 18:40:02

标签: c++ global-variables random-sample static-variables

变量"初始化"在方法initRandomSeed()

/*
 * File: random.cpp
 * ----------------
 * This file implements the random.h interface.
 */


#include <cstdlib>
#include <cmath>
#include <ctime>
#include "random.h"
#include "private/randompatch.h"
using namespace std;

/* Private function prototype */

static void initRandomSeed();

/*
 * Implementation notes: randomInteger
 * -----------------------------------
 * The code for randomInteger produces the number in four steps:
 *
 * 1. Generate a random real number d in the range [0 .. 1).
 * 2. Scale the number to the range [0 .. N) where N is the number of values.
 * 3. Translate the number so that the range starts at the appropriate value.
 * 4. Convert the result to the next lower integer.
 *
 * The implementation is complicated by the fact that both the expression
 *
 *     RAND_MAX + 1
 *
 * and the expression for the number of values
 *
 *     high - low + 1
 *
 * can overflow the integer range.  These calculations must therefore be
 * performed using doubles instead of ints.
 */

int randomInteger(int low, int high) {
   initRandomSeed();
   double d = rand() / (double(RAND_MAX) + 1);
   double s = d * (double(high) - low + 1);
   return int(floor(low + s));
}

/*
 * Implementation notes: randomReal
 * --------------------------------
 * The code for randomReal is similar to that for randomInteger,
 * without the final conversion step.
 */

double randomReal(double low, double high) {
   initRandomSeed();
   double d = rand() / (double(RAND_MAX) + 1);
   double s = d * (high - low);
   return low + s;
}

/*
 * Implementation notes: randomChance
 * ----------------------------------
 * The code for randomChance calls randomReal(0, 1) and then checks
 * whether the result is less than the requested probability.
 */

bool randomChance(double p) {
   initRandomSeed();
   return randomReal(0, 1) < p;
}

/*
 * Implementation notes: setRandomSeed
 * -----------------------------------
 * The setRandomSeed function simply forwards its argument to srand.
 * The call to initRandomSeed is required to set the initialized flag.
 */

void setRandomSeed(int seed) {
   initRandomSeed();
   srand(seed);
}

/*
 * Implementation notes: initRandomSeed
 * ------------------------------------
 * The initRandomSeed function declares a static variable that keeps track
 * of whether the seed has been initialized.  The first time initRandomSeed
 * is called, initialized is false, so the seed is set to the current time.
 */

static void initRandomSeed() {
   static bool initialized = false;
   if (!initialized) {
      srand(int(time(NULL)));
      initialized = true;
   }
}
  

确保每次都不执行初始化代码   时间,你需要一个布尔标志来记录是否初始化   已经完成了。不幸的是,宣布这一点并不起作用   flag作为全局变量,因为C ++没有指定顺序   哪些全局变量已初始化。如果你声明其他全局   初始值由随机产生的值   库,没有办法确保初始化标志   已经设置正确。

你能给出一个例子&#34;如果你宣布其他全球性的话 初始值由随机产生的值 库,没有办法确保初始化标志 设置正确。&#34;

1 个答案:

答案 0 :(得分:0)

您想要搜索静态初始化惨败:https://cryptopp.com/wiki/Static_Initialization_Order_Fiasco

例如,如果要声明全局变量myInt:

int myInt = initRandomSeed();

这将在程序进入main()块之前执行,并且无法保证{my}之前设置initialized