# Thread: random number between 0 and variable value

1. ## random number between 0 and variable value

Hey guys,

I'm writing an RPG, and i need to generate a random number between 0 and a certain values content.

eg.
Code:
```x = 10;

random = (rand()x); //to generate a number between 0 and 10```
That doesn't seem to work, but it gives you an idea of what i want to do, i've searched, but a lot of stuff comes up when you enter random numbers!

Thanks.

-HM

2. Code:
`rand() % x; //???`
Oh and make sure to seed before you do that.

3. thanks for the speedy reply, i knew it would be as simle as that :-/

btw, i always seed.

4. Originally posted by HybridM
thanks for the speedy reply, i knew it would be as simle as that :-/

btw, i always seed.
Just reminding you since you didn't seem to include that in your demostration.

5. What is seeding? Why seed?

6. Code:
`srand(time(NULL));`
makes the numbers more random...

7. Code:
```#include <time.h>
#include <stdlib.h>

...

srand(time(NULL));
x=(rand()%(N+1)); // Generate random # between 0 and N
x=(rand()%(end-start+1)+start); // Generate random # between start and end

...```
Hope this helps...

** edit..fixed typo

8. ## Accually...

Code:
`x=(rand()%N);`
That would generate a random number between 0 and N-1.

9. But I thought % <- was a modulus... how does this have to do with a random number?

Other then that... what is the difference betwem rand() and srand() along with what does this line do: srand(time(NULL));

10. Originally posted by Munkey01
Code:
`x=(rand()%N);`
That would generate a random number between 0 and N-1.
oops...yep...you are right...damn typos *edited to fix*

srand() seeds the random # generator with a value. If you do not srand() before you generate numbers then the same 'random' numbers might show up each time your program runs.
EX:
c:\> text.exe
r1=4
r2=10
r3=1

c:\> text.exe
r1=4
r2=10
r3=1

I hope this explains it...the numbers are 'random' but only within a single execution (if it is not seeded via srand()) because it uses that same value as the seed value each time. Seeding it with something out of time() is the best way we can seed it with a different seed each time.

11. Originally posted by biz
Code:
```#include <time.h>
#include <stdlib.h>

...

srand(time(NULL));
x=(rand()%(N+1)); // Generate random # between 0 and N
x=(rand()%(end-start+1)+start); // Generate random # between start and end

...```
Hope this helps...

** edit..fixed typo
Sorry, that didnt help much. What does srand() and time(NULL) do? It would be great if i just knew what those functions do.

12. srand() seeds rand
time(NULL) seeds ran with time

sorry that's the best i can do, someone else can explain better

13. rand() produces a series of numbers based on a starting number (it uses the previously generated number to generate the next number). You can change this starting number using srand(). If the starting number is always the same you'll get the same series of numbers each time. If you seed it with a different number (which time() gives you) you'll get a different series each occurance (assuming you're not seeding rand() at exactly the same time).