# random number between 0 and variable value

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-03-2003
HybridM
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
• 02-03-2003
Munkey01
Code:

`rand() % x; //???`
Oh and make sure to seed before you do that.
• 02-03-2003
HybridM
thanks for the speedy reply, i knew it would be as simle as that :-/

btw, i always seed. :)
• 02-03-2003
Munkey01
Quote:

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.
• 02-03-2003
Panopticon
What is seeding? Why seed?
• 02-04-2003
HybridM
Code:

`srand(time(NULL));`
makes the numbers more random...
• 02-04-2003
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
• 02-04-2003
Munkey01
Accually...
Code:

`x=(rand()%N);`
That would generate a random number between 0 and N-1.
• 02-04-2003
Zahl
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));
• 02-04-2003
biz
Quote:

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.
• 02-04-2003
Panopticon
Quote:

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.
• 02-04-2003
HybridM
srand() seeds rand
time(NULL) seeds ran with time

sorry that's the best i can do, someone else can explain better
• 02-04-2003
JoeSixpack
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).
• 02-04-2003
Eibro
• 02-05-2003
Zahl
the word "seeds" seems strange to me. Plz help me out. (Denmark)

Oh, and besides that... what is the point of modulus in all this?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last