# Rand() questions

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-03-2003
mill1000
Rand() questions
does rand()%11 give me a number between 1-10?
• 05-03-2003
Brighteyes
No, it gives you a number 0-10.
• 05-03-2003
7stud
And it reduces the randomness of the numbers.
• 05-03-2003
Geo-Fry
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
• 05-03-2003
Jamsan
Code:

```#include <time.h> ..... srand(time(0));```
• 05-03-2003
Hammer
It's a secret.
Don't tell anyone
• 05-03-2003
dsig111
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; }```
• 05-03-2003
Hammer
Code:

```  Posted by dsig111   Numb = (rand () % 10) + 1;  while (Numb > 10)  {   Numb--;  }```
What is that supposed to achieve?

>>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?
• 05-03-2003
dsig111
I forgot got thrown off by the %11
• 05-03-2003
mill1000
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;`
• 05-03-2003
Hammer
First off, did you read the two links I gave you?

>>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.
• 05-03-2003
dsig111
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...

Code:

```srand (time (NULL)); //must have stdlib.h and time.h int rnum = (rand () % 10) + 1;```
the above will produce trully random values of 1 -10.
• 05-03-2003
dsig111
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; }```
• 05-03-2003
Hammer
>>srand (time (NULL)); twice
You shouldn't be calling srand() twice in the same program. This is also in the FAQ.
• 05-03-2003
mill1000
Im so confused yes i did go to the links but can some one simplify the srand and time i dont get it
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last