Sign in to follow this  
KG_Brad

"rand()" function not working...

Recommended Posts

For about a day now, I've been trying to use the "rand()" function to display a random pre-defined message when players exit the game, you know, similar to Wolfenstein 3D or Doom. But every time I exit, I get the same message. Here's my code:
int exit_message()
{
	//Randomly select message
	which_message = rand()%10+1;
	
	//Display message
	if (which_message == 1) textout_centre_ex(screen,font,EX_MESSAGE1,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 2) textout_centre_ex(screen,font,EX_MESSAGE2,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 3) textout_centre_ex(screen,font,EX_MESSAGE3,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 4) textout_centre_ex(screen,font,EX_MESSAGE4,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 5) textout_centre_ex(screen,font,EX_MESSAGE5,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 6) textout_centre_ex(screen,font,EX_MESSAGE6,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 7) textout_centre_ex(screen,font,EX_MESSAGE7,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 8) textout_centre_ex(screen,font,EX_MESSAGE8,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 9) textout_centre_ex(screen,font,EX_MESSAGE9,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	else if (which_message == 10) textout_centre_ex(screen,font,EX_MESSAGE10,SCREEN_W/2,SCREEN_H/2,WHITE,-1);
	
	//Exit
	readkey();
	return 0;
}
I don't know why it doesn't work. Is there something wrong with my code?

Share this post


Link to post
Share on other sites
Do you call srand(time(0)); once at the beginning of your program. Calling it more than once or not calling it at all will result in having the same random number sequence.

Consider using arrays to store your quit messages:

const char *quitMessages[] = {
"super-cool quit message #1!",
"super-cool quit message #2!",
"super-cool quit message #3!",
/* skip a few */,
"super-cool quit message #9!",
"super-cool quit message #10!",
};

int exit_message()
{
int index = (rand() % 10) + 1
textout_centre_ex(screen,font,quit_messages[i],SCREEN_W/2,SCREEN_H/2,WHITE,-1);
}

Share this post


Link to post
Share on other sites
Quote:
Do you call srand(time(0)); once at the beginning of your program.
No, I didn't call srand...

Yes, I did have them stored in an array, but it was kind of a hassle to add more messages. Now I just use #define...

EDIT: I called srand(time(0)) before rand()%10+1 and now it works. Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by KG_Brad
EDIT: I called srand(time(0)) before rand()%10+1 and now it works. Thanks!
I believe (rand()%10) + 1 is what you are looking for.
A random number between 1 and 10, not 0 and 10.

Share this post


Link to post
Share on other sites
Quote:
(rand()%10) + 1
It did the same thing as rand()%10+1. I did a Google search and found that it doesn't matter if you use parenthesis or not, it just depends on the programmer's own preference.

Share this post


Link to post
Share on other sites
the modulus operator has higher precedence than +. that is why it
( rand() % 10 ) + 1 means the same as rand() % 10 + 1

Share this post


Link to post
Share on other sites
Quote:
Original post by KG_Brad
Quote:
Do you call srand(time(0)); once at the beginning of your program.
No, I didn't call srand...

Yes, I did have them stored in an array, but it was kind of a hassle to add more messages. Now I just use #define...

EDIT: I called srand(time(0)) before rand()%10+1 and now it works. Thanks!
#defines are bad. Given that your original code was frankly a bit of a W.T.F., I shudder to think what it would be like with #defines. rip-off kinda showed the preferred solution.

Here's the way I'd do it:
const char *quitMessages[] = {
"super-cool quit message #1!",
"super-cool quit message #2!",
"super-cool quit message #3!",
/* skip a few */
"super-cool quit message #10!"
};

int exit_message()
{
int index = rand() % (sizeof(quitMessages) / sizeof(quitMessages[0]));
textout_centre_ex(screen,font, quit_messages[index], SCREEN_W/2, SCREEN_H/2, WHITE, -1);
}


You never even have to tell it how many there are. You can simply add more to the array and they will be automatically included and work perfectly![cool]

Share this post


Link to post
Share on other sites
Quote:
#defines are bad. Given that your original code was frankly a bit of a W.T.F., I shudder to think what it would be like with #defines. rip-off kinda showed the preferred solution.
Damn, I didn't know my code was that bad! I'll try rip-off's way if it would make it better.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this