How to generate random numbers on intervals

Is there any basic formula?
Id like to know how do i random up numbers on interval (-201,201) and [-205,205)
Rand() return random numbers from 0.
Generate number from 0 to 402 and subtract 201

Same approach for -205 to 205
Last edited on
So, basically as was just described it would look something like this:

1
2
3
4
5
6
7
8
9
//create random int between a - b | only works on positive numbers
int GetTrueRandomInt(int a, int b)
{
	if (a >= b)	// bad input
		return a;

	int c = (b - a) + 1;
	return rand() % c + a;
}


If you wanted to get a random number using negative numbers then you would change the function to get the absolute value of c.
Topic archived. No new replies allowed.