我使用的是stackoverflow样板文件(修改了变量名):
std::random_device seed;
std::mt19937 gen(seed());
std::uniform_int_distribution<> d6(1, 6);
std::cout << std::endl << "roll = " << d6(gen); //verifies that the basic generator works
在main()中,这个方法很好用,我想做的是写一个简单的函数,它将得到一个想要的骰子数,掷骰子,把骰子显示满足某个条件的结果的次数加起来,然后把这个数作为一个整数返回,我还没有找到任何参数类型或返回类型的组合,可以在不抛出错误的情况下完成这个任务。
示例函数(为简洁起见,使用了一些伪代码):
int dice ( int Pn )
{
int hit = 0;
for loop ( starting from 0, exit @ Pn )
{
if d6(gen) > 3 hit++;
if d6(gen) == 6 hit++; //these conditions are arbitrary and only included for completeness
}
return hit;
}
显然,现在我需要给我的函数dice传递一些参数,这样它就可以访问发行版和生成器了--我还没有找到任何不会抛出错误的类型组合。
以下是一个完整的此类尝试的例子:
#include <iostream>
#include <random>
int dice ( std::uniform_int_distribution<int> , std::mt19937 G , int Pn );
int main()
{
std::random_device seed;
std::mt19937 gen(seed());
std::uniform_int_distribution<> d6( 1, 6 );
std::cout << std::endl << "roll = " << d6(gen);
dice ( d6 ( 1 , 6 ) , 12 ); // Roll 12 dice - arbitrary
return 0;
}
int dice ( std::uniform_int_distribution<int> R , std::mt19937 G , int Pn )
{
int hit = 0;
int num_dice = Pn;
for ( int i = 0; i < num_dice; i++ ) // Pn is the number of dice we want to roll
{
if ( R(G) > 3 ) hit++;
if ( R(G) == 6 ) hit++; //these conditions are arbitrary and only included for completeness
}
return hit;
}
以下是此尝试产生的错误:
main.cpp:13:10: error: no matching function for call to object of type 'std::uniform_int_distribution<>'
dice ( d6 ( 1 , 6 ) , 12 ); // Roll 12 dice - arbitrary
^~
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__random/uniform_int_distribution.h:207:39: note: candidate function template not viable: no known conversion from 'int' to 'const param_type' for 2nd argument
template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__random/uniform_int_distribution.h:205:39: note: candidate function template not viable: requires single argument '__g', but 2 arguments were provided
template<class _URNG> result_type operator()(_URNG& __g)
^
1 error generated.
1条答案
按热度按时间b1zrtrql1#
dice()
需要分发对象本身和一个生成器对象,但是您传递给它的是调用分发的结果,而根本没有生成器。尝试更改此设置:
dice ( d6 ( 1 , 6 ) , 12 );
改为:
dice ( d6, gen, 12 );