2016-07-18 6 views
0

Ich habe erst vor kurzem angefangen, C++ zu lernen, also habe ich Probleme, mein Programm zu debuggen.Viele Compilerfehler beim Versuch, random_device, mt19937 und uniform_int_distribution zu verwenden

Ich möchte Zufallszahlen generieren. Nach einigen googeln scheint es, dass eine Möglichkeit, das zu tun, etwas entlang der Linien von

#include <random> 

std::mt19937 rng; 
std::uniform_int_distribution<int> distribution; 

int main() 
{ 
    rng(std::random_device()()); 
    distribution(0, 10); 
} 

jedoch ist, wenn ich kompilieren, dass mit g++ -std=c++14 test.cpp ich diese Fehler erhalten:

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); 
        ^

habe ich einige der Forschung in random_device, mt19937 und uniform_int_distribution, und mit meinem begrenzten Wissen scheint es, dass ich alles richtig verwende. Ich kopiere auch den Code von anderen Antworten hier auf SO, also scheint es, dass der Code funktionieren sollte. Hat das Ändern des Kontext des Codes etwas verändert?

+0

Sie deklarieren einige Dinge und versuchen, sie später aufzurufen. Wenn Sie ihre Konstruktoren verwenden möchten, tun Sie dies, wo sie deklariert sind. 'std :: mt19937 rng (std :: random_device());' – Ryan

+0

Was versuchen Sie mit 'rng (std :: random_device()());'? Rufen Sie den Konstruktor auf? Das ist schon geschehen, bevor "main" gestartet wurde ... Sie müssen den Destruktor an der Stelle aufrufen, an der Sie das 'rng'-Objekt deklarieren. – Cornstalks

+0

'rng.seed (std :: random_device()());' und 'distribution.param (std :: uniform_int_distribution :: param_type (0,10));' – WhozCraig

Antwort

2

Sie vermischen das Konzept von Gerät, Generator und Verteilung. Versuche, alles getrennt zu halten; dann wird es einfach werden.

#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'; 
}