studying for test

closed account (ETA9216C)
I'm studying for a test. How would you code a simple random generator for dice rolling?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
unsigned int generateRandomNumber(unsigned int maxValue, unsigned int minValue = 1)
{
    if (maxValue == minValue) return maxValue;
    if (maxValue < minValue)
    {
        int temp = maxValue;
        maxValue = minValue;
        minValue = temp;
    }

    static bool hasBeenSeeded = false;
    if(!hasBeenSeeded)
    {
        std::srand(std::time(nullptr));
        hasBeenSeeded = true;
    }

    return (std::rand() % (maxValue - minValue + 1)) + minValue;
}
closed account (ETA9216C)
Thank you, I try to make this as good reference. And HOPE i pass my test.
Topic archived. No new replies allowed.