random number with a range

Hey guys I have a quick question about generating a random number with a range between 800-4000

I have seen a few examples that say I should do this

 
randomlocation = rand() %800 + 3200;


unsure if that would work or not. Also in my code do I only need one srand(unsigned int time (0)) or do I need to have the srand at every random number generator?

thanks in advance guys
1) Look up what % does and you should be able to figure it out yourself.

2) You only need one srand() call in your program.
I have seen a few examples that say I should do this
randomlocation = rand() %800 + 3200;


That would give you a random number between 3200 and 3999.

Given a minimum value and a maximum value:

1
2
3
4
5
unsigned getRandom( unsigned min, unsigned max )
{
    unsigned range = max-min+1 ;
    return rand() % range + min ;
}



Also in my code do I only need one srand(unsigned int time (0))

You need only seed the generator once per program.
@ firedraco - thanks I read about it gave me some more insight

@ cire - thanks for the help it worked out great
Topic archived. No new replies allowed.