• Advertisement

Archived

This topic is now archived and is closed to further replies.

maybe stupid question in C

This topic is 5938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just want to know if there is a function in C that gives me the possibility to give a starting number and an ending number and that the computer tries to find a number between it randomly. If possible everytime another one. Thx Hardware: the parts of a computer that can be kicked Software: the parts of a computer that you wnat to kick

Share this post


Link to post
Share on other sites
Advertisement
Easy as pie:

int highestnumber=25; // or whatever numbers you want
int lowestnumber=13;

int randomnumber=rand()%(highestnumber-lowestnumber)+lowestnumber;

Share this post


Link to post
Share on other sites
seems like it''s not working, did this

#include

void main(){

int highestnumber = ''10'';
int lowestnumber = ''1'';
int computer_guess;

computer_guess = rand()%(highestnumber - lowestnumber) + lowestnumber;

printf("%d", computer_guess);

}

and he''s always coming up with 90.

can someone explain?

Hardware: the parts of a computer that can be kicked
Software: the parts of a computer that you wnat to kick

Share this post


Link to post
Share on other sites
Your assigning char''s to int''s

int highestnumber = ''10'';
int lowestnumber = ''1'';

Try this...

int highestnumber = 10;
int lowestnumber = 1;

Share this post


Link to post
Share on other sites
tried it, but now it''s always coming up with 6?
is this normal or am I missing something?

Share this post


Link to post
Share on other sites
Did you call srand(); ?

  
#include

void main(){

int highestnumber = 10;
int lowestnumber = 1;
int computer_guess;

srand(time(0));
computer_guess = rand()%(highestnumber - lowestnumber) + lowestnumber;

printf("%d", computer_guess);

}


You'll have to include time.h for this to work.

------------------------------------------------------------
"To a computer, chaos is just another kind of order."

Edited by - Profus on November 23, 2001 6:55:13 AM

Edited by - Profus on November 23, 2001 6:56:20 AM

Share this post


Link to post
Share on other sites
you''ve got

  
computer_guess = rand()%(highestnumber - lowestnumber) + lowestnumber;


your missing some brackets in there

  
computer_guess = (rand()%(highestnumber - lowestnumber)) + lowestnumber;


zipless

Share this post


Link to post
Share on other sites
No I didn''t

Thanks to all that replied me and that helped me come out of this problem :-)

now all working fine :-)

Share this post


Link to post
Share on other sites

  • Advertisement