Based off of some articles I read, I created a dice roll function as thus:
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
short roll_dice(short number_of_dice, short die_strength) {
short all_rolls_sum = 0;
short individual_roll = 0;
for (short i = number_of_dice; i > 0; i--) {
srand(time(NULL));
individual_roll = (rand() % die_strength) +1;
all_rolls_sum += individual_roll;
}
return all_rolls_sum;
}
The program I used to run this function is:
int main() {
cout << roll_dice(1, 6);
return 0;
}
This is meant to simulate a 1d6 die roll. The results were what I expected (i.e. 2, 4, 3, 5, 6, 4, 4, 1) returning both odd numbers and even numbers. However, the problem is that, when I input "2" for the first parameter, I am only getting even numbers returned (i.e. 2, 2, 6, 8, 6, 4, 4, 8). It turns out that any time I input an odd number for the first parameter (number_of_dice), I get both even and odd numbers returned. If I input an even number for the number_of_dice, I get only even.
I am still relatively new to C++/programming but is this an issue with the modulo being used in the (rand() % die_strength) +1
portion of my code?
Aucun commentaire:
Enregistrer un commentaire