weixin_42722475 2018-10-04 05:05
浏览 1335

关于random的问题,以给定概率返回0/1.

输入给定per,当per小于0.2 时,最后a的个数达到给定值的概率反而变成0了,一直没搞懂,求指点

代码如下
#include
#include "random.h"
#include "console.h" // for GWindow
#include "simpio.h" // for getLine
using namespace std;

int main ()
{

int voters=getInteger("Enter number of voters");
double can=getDouble("Enter percentage spread between candidates");
double per=getDouble("Enter voting error percentage");
int a=0;
double inv=0;
for(int j=0;j<500;j++){
    for(int i=0;i<voters*can;i++){
        if(randomChance(per)) a++;
    }
    if(a>=voters*can/2) inv++;
    a=0;

}
double ratio=1.0*inv*100/500;
cout<<ratio<<"%";
return 0;
}

random.h:

#include "random.h"
#include
#include
#include
#include
#include "private/static.h"

/* Private function prototype */

static void initRandomSeed();

/* internal buffer of fixed random numbers to return; used by autograders */
STATIC_VARIABLE_DECLARE_COLLECTION_EMPTY(std::queue, fixedBools)
STATIC_VARIABLE_DECLARE_COLLECTION_EMPTY(std::queue, fixedInts)
STATIC_VARIABLE_DECLARE_COLLECTION_EMPTY(std::queue, fixedReals)

namespace autograder {
void randomFeedBool(bool value) {
STATIC_VARIABLE(fixedBools).push(value);
}

void randomFeedClear() {
STATIC_VARIABLE(fixedBools) = std::queue();
STATIC_VARIABLE(fixedInts) = std::queue();
STATIC_VARIABLE(fixedReals) = std::queue();
}

void randomFeedInteger(int value) {
STATIC_VARIABLE(fixedInts).push(value);
}

void randomFeedReal(double value) {
STATIC_VARIABLE(fixedReals).push(value);
}
}
/* end 'fixed' internal stuff */

bool randomBool() {
return randomChance(0.5);
}

/*

  • 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) { if (!STATIC_VARIABLE(fixedBools).empty()) { bool top = STATIC_VARIABLE(fixedBools).front(); STATIC_VARIABLE(fixedBools).pop(); return top; } initRandomSeed(); return randomReal(0, 1) < p; }

int randomColor() {
if (!STATIC_VARIABLE(fixedInts).empty()) {
int top = STATIC_VARIABLE(fixedInts).front();
STATIC_VARIABLE(fixedInts).pop();
return top & 0x00ffffff;
}
initRandomSeed();
return rand() & 0x00ffffff;
}

// don't want to depend on gwindow.h
extern std::string convertRGBToColor(int rgb);

std::string randomColorString() {
return convertRGBToColor(randomColor());
}

/*

  • 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) { if (!STATIC_VARIABLE(fixedInts).empty()) { int top = STATIC_VARIABLE(fixedInts).front(); STATIC_VARIABLE(fixedInts).pop(); if (top < low || top > high) { // make sure the value is in the given range // (assumes that low/high don't overflow int range) int range = high - low + 1; top = low + std::abs(top) % range; } return top; } 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) { if (!STATIC_VARIABLE(fixedReals).empty()) { double top = STATIC_VARIABLE(fixedReals).front(); STATIC_VARIABLE(fixedReals).pop(); return top; } initRandomSeed(); double d = rand() / (double(RAND_MAX) + 1); double s = d * (high - low); return low + s; }

/*

  • 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(nullptr))); rand(); // BUGFIX: throwaway call to get randomness going _initialized = true; } }

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      悬赏问题

      • ¥15 编写Linux测试服务启动失败
      • ¥20 matlab绘冲床平面连杆机构图和仿真,求帮助
      • ¥15 为什么树莓派5b显示禁止连接
      • ¥15 请专家处理报错!基于深度学习的车型分类问题:数据集为包含 10 种车型的图像数据集,分为训练集、验证集和测试集。
      • ¥20 流量太费!寻找便宜的app音视频SDK或平替方案。
      • ¥15 kubeasz部署遇到问题
      • ¥15 GUIDE to App Designer Migration Tool for MATLAB
      • ¥50 第三代非支配排序遗传算法(NSGA-Ⅲ)和多目标粒子群优化算法(MOPSO)的实现
      • ¥20 plant simulation与python com接口实时数据交互
      • ¥15 有关汽车的MC9S12XS128单片机实验
      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部