# Changing Random Numbers?

• 04-22-2002
napkin111
Changing Random Numbers?
How do I make a function that creates a different random number each time the function is called?

Thanks,
napKIN
• 04-22-2002
Leeman_s
first, you need.to.include.<stdlib.h>.for.rand();.Then.put.an ywhere.in.your.program,.....'srand(time(NULL));'.. ...that.will.make.a.new.random.number.each.time.Yo u.must.include.<time.h>.for.that.BTW....I.have.to. use.periods.because.here.at.school.the.space.bar.i s.not.working.now.
• 04-22-2002
biosx
Code:

```#include <iostream>        // cout #include <time>            // time() #include <cstdlib>          // srand() & rand() using namespace std; int main() {   int randNum;   srand( time(0) );      // or srand( time(NULL) )   randNum = rand();   cout << "Your random number is " << randNum << endl;   return 0; }```
Each time this program runs it should have a different set of random numbers. The srand( ) function seeds the rand( ) with the time in seconds since 1972 (erh something). Anyway, the srand( ) call is the key to making it generate different sets of numbers each time.

Good luck.
• 04-22-2002
napkin111
Thanks alot guys.
Leeman, I'm at school to....only my space bar works;)

napKIN
• 04-22-2002
napkin111
How can I make it so it creates a random number up to a max. I'm going to use it to deal weapon damage in an RPG. Something like this:

code:
------------------------
damage=rand(5)+10;
------------------------
End code

so that the weapon deals from 10-15 damage.

thanks,

napKIN
• 04-22-2002
Leeman_s
low + rand() % (high-low+1)

example for between 1 and 10:

damage=1+rand()%(10-1+1); //1 through 10
• 04-23-2002
napkin111
Thanks, but I don't understand a couple things...

1. I forget what % means(doh!), I'll check that when I get home though
2. Why "(10-1+1)"? Isn't that the same as 10?

Thanks Leeman

napKIN