关于C++中伯努利分布bernoulli_distribution的使用,求助!

问题描述

关于C++中伯努利分布bernoulli_distribution的使用,求助!

如题,我是有一个double prob,作为伯努利分布的p值。
每次如果bool值是true,就是succeed;反之则不是。
函数如下。

 bool ifSucceed(double prob)
{
    default_random_engine generator;
    bernoulli_distribution b(prob);
    return b(generator);
}

调用时:

 for (int i = 0; i < n - 1; ++i)
    {
        if (ifSucceed(cSetx[i].probFailure))
        {
            cout << i<<": Twice Delivery" << endl;
            totalDist += 3*getDist(cSetx[i], cSetx[i + 1]);
        }
        else
        {
            totalDist += getDist(cSetx[i], cSetx[i + 1]);
            //cout << "TotalDist: " << totalDist << endl;
        }

但是试验了几次,都没有出现succeed的情况。
就算把部分概率改为1,那一部分也不一定能出现succeed,这显然是不合理的。

请各位赐教!谢谢!

解决方案

generator和b应该放在循环的外面,在固定设置一个概率值后,打印循环调用伯努利分布的结果值,例如:

std::default_random_engine generator;
std::bernoulli_distribution distribution(0.5);
int count=0;

for (int i=0; i<10000; ++i)
if (distribution(generator)) ++count;
std::cout << "true: " << count << std::endl;
std::cout << "false: " << nrolls-count << std::endl;
-------------------结果----------------------
true: 4994
false: 5006

时间: 2024-08-03 04:55:52

关于C++中伯努利分布bernoulli_distribution的使用,求助!的相关文章

new-这是一个有关于C++的问题,作业中遇到的问题,求助!

问题描述 这是一个有关于C++的问题,作业中遇到的问题,求助! read one string from the file called ""infile.txt"" into your string class using your read member function()The class will store the string in dynamic memory that is pointed to with the pointer. When you

ndk ndroid tudio jni-AndroidStudio中开发ndk报错,求助!!!

问题描述 AndroidStudio中开发ndk报错,求助!!! D:workspacesAndroidProjectsGISpeechappsrcmainjniHTKLibexcpt.h:20:21: fatal error: crtdefs.h: No such file or directory #include <crtdefs.h> as 的项目中使用ndk 开发,其中有些 c 文件中引用了一些 系统平台下的 h 文件,在 vs2010 中可以调通,但是加入到 jni 中就报错,请问

cocos2d-x移植到eclipse中main.cpp报错 求助!

问题描述 cocos2d-x移植到eclipse中main.cpp报错 求助!

oracle中sql查询语句,求助

问题描述 oracle中sql查询语句,求助 比如有两张表 用户表User {id name }成绩表Score {id userid coursename courseScore}现在怎么查找每个用户的最高成绩 解决方案 select distinct u.idu.names.courseScore from User as uinner join ( select max(courseScore) as courseScore userid from Scoregroup by userid

ios-mac中垃圾筒中误删清空,怎么恢复?求助

问题描述 mac中垃圾筒中误删清空,怎么恢复?求助 x-code工程中文件出现无限循环,之后删除到垃圾桶了,然后手贱又清空了,现文件找不到了,工程无法运行买很多核心代码丢失,求助,怎么办? 解决方案 这个就真的丢失了,找不回来 解决方案二: 你代码备份了么?从备份里面找.没有备份自己认了吧. 解决方案三: 去售后 做个数据恢复 解决方案四: Linux服务器误删恢复

android-showasaction在item中使用的问题,求助!

问题描述 showasaction在item中使用的问题,求助! 在item中用android:showasaction会报错 而用app:不会错 解决方案 android的RecycleView的基本使用和item间隔问题使用Toolbar后showAsAction无效的问题解决使用Fragment作为Item的ViewPager不更新问题.

extjs 中grid多行插入求助,

问题描述 handler:function(){varp=newRecord({id:'',name:'',descn:''});grid.stopEditing();varn=subwingrid.getStore().getCount();alert(n);store.insert(0,p);grid.startEditing(0,0);}这是grid的单行插入,多行插入怎么就不行啦呀我用的方法是functioninsert1(){subwingrid.stopEditing();//for

求助,为什么font-weight没有作用??

问题描述 求助,为什么font-weight没有作用?? 字体加不了粗啊 h1 { color:white; font-family:YouYuan; font-size:30px; font-weight:900; margin:15px; } " 数据库管理界面 " 解决方案 布局文件中layout_weight的作用 解决方案二: h1默认就是bold(700+)了,设置为300或者lighter 相关文章 求助一道画ER图的题目 函数作用域-怎么把变量传入事件函数中,求助 sq

c程序中如何动态创建db2临时表

问题描述 c程序中如何动态创建db2临时表 c程序中如何动态创建db2临时表?因为where条件是动态可变,需要查阅2-4张表中数据,因此想做个临时表,根据动态的where条件,动态生成临时表,这样提高查询效率.不知程序中应该怎么写,求助各位大神,谢谢!