-1

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.

  • 2
    **WARNING**: Using [`rand()` is highly problematic](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) and you’re strongly encouraged to use an appropriate [random number generator facility in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random) that produces high-quality random values. Your use of `time(NULL)` as a random number seed means that this will produce identical results if run in the same second, and on many platforms `rand()` is [*barely* random at all](http://dilbert.com/strip/2001-10-25). – tadman Nov 15 '19 at 18:33
  • this is the single most asked question on the topic of random numbers. https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once – 463035818_is_not_an_ai Nov 15 '19 at 18:33
  • `srand(time(NULL))` is a really crappy way to seed your generator. Everything within the same second will generate the same sequence. Also, `rand` is not exactly the tip of the random mountain, better use something from [random](https://en.cppreference.com/w/cpp/numeric/random) if you want high quality random numbers. – Jesper Juhl Nov 15 '19 at 18:36
  • Move your seed to `main()` and see what happens. –  Nov 15 '19 at 18:37
  • @JesperJuhl so i changed my random function, and I put a pseudo-random number generator in. For whatever reason, it is expecting a semicolon on line 32. I must have looked this over twenty times along with a separate set of eyes...do you know why? (I will copy my new code into the post.) – liammanchester Nov 15 '19 at 18:52

1 Answers1

0

Random number generators based on time need to be seeded once and then used multiple times if you're doing a loop like that. Seed it once before the loop, get random numbers many times in the loop, then they won't be the same.

Klaycon
  • 10,599
  • 18
  • 35
  • They will still be the same if multiple programs are started within the same second (an ice age in CPU terms), when using a second resolution based source as the seed. – Jesper Juhl Nov 15 '19 at 18:40
  • This edge case seems pretty far disconnected from the context of the question being asked – Klaycon Nov 15 '19 at 18:41