does rand()%11 give me a number between 1-10?
does rand()%11 give me a number between 1-10?
No, it gives you a number 0-10.
p.s. What the alphabet would look like without q and r.
And it reduces the randomness of the numbers.
i dont know exactly what to do, but having srand( GetTickCount() ); somewhere before it makes it random based on the time, so the chance of getting the same random number is much lower. but i dont know exactly how to do it
"You can lead a man to Congress, but you can't make him think."
"The Grand Old Duke of York
-He had ten thousand men.
-His case comes up next week."
"Roses are red, violets are blue, I'm schizophrenic, and so am I."
"A computer once beat me at chess, but it was no match for me at kick boxing."
"More and more of our imports are coming from overseas."
--George W. Bush
"If it weren't for electricity, we'd all be wacthing TV by candlelight."
--George W. Bush
Code:#include <time.h> ..... srand(time(0));
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
try this...
Code:#include <time.h> #include <stdlib.h> int main (void) { int Numb; srand (time (NULL)); Numb = (rand () % 10) + 1; while (Numb > 10) { Numb--; } return 0; }
“The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum
What is that supposed to achieve?Code:Posted by dsig111 Numb = (rand () % 10) + 1; while (Numb > 10) { Numb--; }
>>rand() % 10
will give a number between 0 and 9. You then add one to it, so effectively, it's giving a number between 1 and 10.
So what's the "while" loop for?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I forgot got thrown off by the %11
“The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum
thx but this code produced 9 in the begining and when another function calls the function it is in it rises to 10. any sugjestions on how to stop it from aways being nine and on how to reset it when the function ends
Code:int rnum=rand()%11+1;
First off, did you read the two links I gave you?
Now, from your comments:
>>sugjestions on how to stop it from aways being nine<<
If it's random, it'll do what it wants to. You might get any number between min and max.
>>how to reset it when the function ends<<
You can't "reset" it as such, only reset it back to the start of the psuedo random number list, but that isn't recommend.
If the links don't help you, explain what you're trying to do, and post some of your code.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
the srand function sets a seed to randomize off of and when using time to do it it will set the seed different ly each time and stop using % 11 it returns 0-10 snd when add 1 it returns 1-11...
the above will produce trully random values of 1 -10.Code:srand (time (NULL)); //must have stdlib.h and time.h int rnum = (rand () % 10) + 1;
“The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum
also if you want to prevent it from repeating the last value use a while statement...
Code:#include <time.h> #include <stdlib.h> int main (void) { int rnum, p_rnum; srand (time (NULL)); rnum = (rand () % 10) + 1; /* later on */ do { srand (time (NULL)); p_rnum = rnum; rnum = (rand () % 10) + 1); } while (p_rnum == rnum); return 0; }
“The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum
>>srand (time (NULL)); twice
You shouldn't be calling srand() twice in the same program. This is also in the FAQ.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Im so confused yes i did go to the links but can some one simplify the srand and time i dont get it