I am relatively new to C++, and I'm trying to make a dice roller program that gets the sum of the two dice. What's happening is I am trying to have the user input how many times they want to roll the dice, and that will decide how many times my "roll" function runs and produces a value for each die, and then adds them up to get the sum. The problem is that when it runs the loop more than once, it doesn't re-randomize the numbers. It does randomize the numbers because when I do run it the numbers randomize from the last time I ran the program, but when the loop runs multiple times it keeps the same random numbers.
EDIT: I changed my randomizer to a pseudo-random generator so I would get different numbers every time the loop runs (hopefully), but now for whatever reason I am getting a semicolon expected error on line 32? I have pasted in my new code with the pseudo generator and I have commented where I am getting the semicolon expected error. Anyone know why? (I have looked it over many times and really can't see where it's going wrong.)
EDIT: Thanks everyone for your help, got it! :)
Here is my code:
#include "pch.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <random>
using namespace std;
// Variables
int rolls;
int d1;
int d2;
int minv = 1;
int maxv = 6;
int range = maxv - minv + 1;
// --------------------------------------------------------------
void random() {
mt19937 engine(0); // Fixed seed of 0
uniform_real_distribution<> dist;
for (int i = 0; i < 100; i++) {
cout << dist(engine) << endl;
}
// Function for rolling
void roll() { // EXPECITNG A SEMICOLON HERE
// Random number for each die is generated
d1 = random() % range + minv;
d2 = random() % range + minv;
// -----------------------------------------------------------
// Outputs
cout << "(D1: " << d1 << ") + " << "(D2: " << d2 << ") = " << (d1 + d2) << endl;
cout << endl;
}
int main()
{
// Program displays program header
cout << "--DICE ROLLER--" << endl;
cout << endl;
cout << endl;
// -----------------------------------------------------------
// Number of rolls wanted
cout << "How many times would you like to roll? ";
cin >> rolls;
// -----------------------------------------------------------
// Loop for rolling depending on how many rolls the user wanted
int i = 0;
while (i < rolls) {
i++;
roll();
}
}
Any suggestions? Thanks in advance.