Problem with random generator

I'm making a dice roll system where: The program is to display how many times each possible outcome of rolling the dice actually occurred.
The problem is now, I made some classes where the dice are randomly generated.

This is the random:
int Dice::TrowDice()
{
srand ( time(NULL) );
int randNumber;
randNumber = rand() % 6 + 1;
return randNumber;
}


This makes it possible of trowing with any amount of dices:

int DicesThrow::TossDices(){
Dice dice;
int throwSum=0;
for (int i=0; i<amountDices;++i)
{
throwSum+=dice.TrowDice();
}
return throwSum;
}

Then last, here I put this random hand of dices in a loop. Then I put them in the index so I can see the recurrency of the dices. But this always gives the same result :S

void DiceCalculations::PutInIndex(int* A, int x, int amount)
{
int j = 0;
for (int i=0;i<x;++i)
{
DicesThrow dices(amount);
j=dices.TossDices();

cout << "(PutInIndex " << i << ") " << j << endl;

*(A+j)= *(A+j)+1;
}
}

My question is: how can I manage to get different randoms when I do this loop?

Thanks in advance!!
Last edited on
only call srand once when your program starts.

Do not call it for every generated number.
Thanks worked perfectly!
But now I have an other problem:
void DiceCalculations::PutInIndex(int* A, int x, int amount)
{
int j = 0;
for (int i=0;i<x;++i)
{
DicesThrow dices(amount);
j=dices.TossDices();
cout << "(PutInIndex " << i << ") " <<j << endl;

*(A+j)= *(A+j)+1;
}
}

Puts amount of recurrence in an array, but when I print it of it gives random numbers.

Normally this should give a normal amount. I think the problem lays with:
*(A+j)= *(A+j)+1; //this should add 1 to the amount of that element
How can I fix this?


Is there a reason you don't just write
A[j]= A[j]+1; - does the same thing, just that it's a lot more readible.
Yes I changed it to A[j]++;
I also found the problem wich was in my main.

Thanks!
Last edited on
Topic archived. No new replies allowed.