How to change random number amount

Ok so I have a random number generator in main that I use for everything that uses it in the program which are only switch statements. The problem is I have like 3 or 4 different switch statements and each of them are different lengths, so one has 5 cases, one has 4, one has 2. How can I use the one random number generator with all of the switch statements?

I apologize for lack of code, but I have 931 lines of code and I really dont want to post it all, but I'll post my main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main()
{
    vector<int> tItems;
    tItems.push_back(5);

    time_t T;
    unsigned int Time;
    time(&T);
    srand(T);

    for(int i = 0; i < 5; i++)
    {
        Time = rand() % 5;
    }

    Player player(100,
           0,
           0,
           0,
           0,
           500,
           0,
           0,
           tItems);

    Dinosaur dinosaur(100, 5, Time);

    Menu(player, dinosaur);
    //dinoSelect(player, dinosaur);
}
Last edited on
you can use a function to do this. It will make it easier to read and will be less error prone. You would pass the number of cases as arguments to the function, and call the function if the for loop. For example:
1
2
3
4
5
int cases = 5;
for(int i = 0; i < cases; ++i)
{
	int number = randomNumberGenerator(cases);
}


1
2
3
4
5
6
int randomNumberGenerator(int numberOfCases)
{
	int randomNum;	//random number generated
	randomNum = rand() % numberOfCases;	
	return randomNum;
}


Also, using srand(time(NULL)) to set your time is easier to read and interpret.
Topic archived. No new replies allowed.