When I use the random library in functions it doesn't work well at all. it prints inncorrect things like : This and this but if I am not using functions it works completly fine.
I tried to create a function that returns the value of the random number but it fails and prints something weird
Works good:
#include <iostream>
#include <random>
#include <string>
using namespace std;
int main()
{
random_device rd;
mt19937 number(rd());
uniform_int_distribution <>random(1, 3);
int num = random(number);
cout << "The computer choose: " << endl;
if(num == 1){
cout << "one" << endl;
}
if (num == 2) {
cout << "two" << endl;
}
if (num == 3) {
cout << "three" << endl;
}
cout << num; // to check the real number
Doesn't work good:
#include <iostream>
#include <random>
#include <string>
using namespace std;
int random(int num);
int main()
{
int num = 0;
cout << "The computer choose: " << endl;
if(random(num) == 1){
cout << "one" << endl;
}
if (random(num) == 2) {
cout << "two" << endl;
}
if (random(num) == 3) {
cout << "three" << endl;
}
cout << random(num); // to check the real number
}
int random(int num) {
string call;
random_device rd;
mt19937 number(rd());
uniform_int_distribution <>random(1, 3);
num = random(number);
return num;
}
Aucun commentaire:
Enregistrer un commentaire