尝试使用 random_device、mt19937 和 uniform_int_distribution 时出现大量编
Lots of compiler errors when attempting to use random_device, mt19937, and uniform_int_distribution
我最近才开始学习C++,所以我在调试程序时遇到了问题。
我想生成随机数。经过一番谷歌搜索,似乎一种方法是类似于
#include <random>
std::mt19937 rng;
std::uniform_int_distribution<int> distribution;
int main()
{
rng(std::random_device()());
distribution(0, 10);
}
但是,当我使用 g++ -std=c++14 test.cpp
编译它时,我收到以下错误:
test.cpp: In function ‘int main()’:
test.cpp:8:31: error: no match for call to ‘(std::mt19937 {aka std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u>}) (std::random_device::result_type)’
rng(std::random_device()());
^
In file included from /usr/include/c++/5/random:49:0,
from test.cpp:1:
/usr/include/c++/5/bits/random.h:546:7: note: candidate: std::mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type std::mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::operator()() [with _UIntType = unsigned int; unsigned int __w = 32u; unsigned int __n = 624u; unsigned int __m = 397u; unsigned int __r = 31u; _UIntType __a = 2567483615u; unsigned int __u = 11u; _UIntType __d = 4294967295u; unsigned int __s = 7u; _UIntType __b = 2636928640u; unsigned int __t = 15u; _UIntType __c = 4022730752u; unsigned int __l = 18u; _UIntType __f = 1812433253u; std::mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type = unsigned int]
operator()();
^
/usr/include/c++/5/bits/random.h:546:7: note: candidate expects 0 arguments, 1 provided
test.cpp:9:23: error: no match for call to ‘(std::uniform_int_distribution<int>) (int, int)’
distribution(0, 10);
^
In file included from /usr/include/c++/5/random:49:0,
from test.cpp:1:
/usr/include/c++/5/bits/random.h:1768:2: note: candidate: template<class _UniformRandomNumberGenerator> std::uniform_int_distribution<_IntType>::result_type std::uniform_int_distribution<_IntType>::operator()(_UniformRandomNumberGenerator&) [with _UniformRandomNumberGenerator = _UniformRandomNumberGenerator; _IntType = int]
operator()(_UniformRandomNumberGenerator& __urng)
^
/usr/include/c++/5/bits/random.h:1768:2: note: template argument deduction/substitution failed:
test.cpp:9:23: note: candidate expects 1 argument, 2 provided
distribution(0, 10);
^
In file included from /usr/include/c++/5/random:49:0,
from test.cpp:1:
/usr/include/c++/5/bits/random.h:1773:2: note: candidate: template<class _UniformRandomNumberGenerator> std::uniform_int_distribution<_IntType>::result_type std::uniform_int_distribution<_IntType>::operator()(_UniformRandomNumberGenerator&, const std::uniform_int_distribution<_IntType>::param_type&) [with _UniformRandomNumberGenerator = _UniformRandomNumberGenerator; _IntType = int]
operator()(_UniformRandomNumberGenerator& __urng,
^
/usr/include/c++/5/bits/random.h:1773:2: note: template argument deduction/substitution failed:
test.cpp:9:23: note: cannot convert ‘10’ (type ‘int’) to type ‘const std::uniform_int_distribution<int>::param_type&’
distribution(0, 10);
^
我对random_device
、mt19937
和uniform_int_distribution
做了一些研究,以我有限的知识,我似乎正确地使用了一切。我也基本上复制粘贴了SO上其他答案的代码,所以看起来代码应该可以工作。更改代码的上下文是否破坏了某些内容?
你混淆了设备、生成器和配电的概念。尽量把每件事都分开;然后它会变得简单。
#include <random>
#include <iostream>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 200);
for (int n = 0; n < 10; ++n) {
std::cout << dis(gen) << ' ';
}
std::cout << 'n';
}
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 为什么我无法更改"set<set>"循环中的值<int>
- 长 长 int 不要 长 int 好
- C++程序在循环后给出奇怪的int值
- 如何计算数据类型的范围,例如int
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)