Hey i have been playing around with random num gens and have had some issues.
First of all i keep getting a the same string of numbers(i have even tried srand).
And second of all i have had an issue with this switch statement it prints out everything passed the number it gets.
1) Don't call srand() every time you want a number. You should call it exactly once at the start of main. rand() is ok to call multiple times.
2) case blocks in a switch need to end with a break; statement or else code will "bleed through" to the next case.
EDIT:
Also... generally time is used to seed srand... not clock:
srand( (unsigned)time(nullptr) );
This might be why you're getting the same number every time.
Also.... it would make more sense for random() to actually return the random number, rather than set a global and return 0. That way you don't need to make random_integer global at all:
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
int random_number; // no longer needs to be global. Globals are bad - avoid them.
random_number = random(); // grab the return value
//...
}
int random()
{
return rand() % 10; // <- return the value, rather than returning 0
}