rand()

I know about the rand function but how can you get the number to be -50 thought 50? Its like this:
1
2
3
4
srand(time(0)) //something like that
int number;
int number2;
rand() % number + number2
Last edited on
The easiest way is to define two range constants and use those:

1
2
3
4
5
6
const int LOWER = -50;
const int UPPER = 50;

...

int n = LOWER + rand()%(UPPER-LOWER);


This will yield random numbers between LOWER and UPPER, e.g. -50 and 50

but I'm going to be using the rand function a lot and I know that rand() isn't that random every time and is often the same thing if you call it in the same function again. But if you put something in front of it like srand(static_uncast<unsigned_int>(time(0))) but that would won't for my case so what else works?
You should call srand only once, the static_uncast<unsigned_int> part is wrong, it should be static_cast<unsigned int> but it could be done implicitly
thanks Bazzy and Corpus
Topic archived. No new replies allowed.