Search code examples
c++randomsrand

Random Dice not reseeding


I created the following function to create random numbers for a dice game

#include <iostream>
#include <ctime>
#include <cstdlib>
#include "dice.h"
#include "display.h"
using namespace std;
int die[6][10];
void dice(int times, int dice){
    int r;
    for(int x=0;x<times;x++){
        for(int x=0;x<dice;x++){
            srand(time(0));
            r=(rand() % 5);
            die[r][x]+=1;
            cout<<"Die #"<<x+1<<" rolled a "<<r<<endl;
        }
    }

}

It doesn't reseed though. It just outputs the same number for each die. Does anyone know how I could fix it?


Solution

  • srand() in a function that is called repeatedly, or a loop is no good.

    Put your call to srand() in main. Only call it once per program.