does rand()%11 give me a number between 1-10?
Printable View
does rand()%11 give me a number between 1-10?
No, it gives you a number 0-10.
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
Code:#include <time.h>
.....
srand(time(0));
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;
}
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?
I forgot got thrown off by the %11
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.
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;
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;
}
>>srand (time (NULL)); twice
You shouldn't be calling srand() twice in the same program. This is also in the FAQ.
Im so confused yes i did go to the links but can some one simplify the srand and time i dont get it