# Thread: what is the range of rand()

1. ## what is the range of rand()

Does rand() not generate numbers between 0 to 32,767.
source: http://mathbits.com/mathbits/compsci...yFunc/rand.htm

HOwever, when I run the below code, I'm getting huge numbers like:

717154347, 81702301

Code:
```#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
srand((unsigned) time(NULL));
printf("number is: %d", rand());

return 0;
}```

example output

2. It produces numbers in range of its return type, in this case, int as prototyped.

The int type is defined by the standard to be at least two bytes, which gives the signed range of a short, or -32768 through 32768. However, on most modern computers, it's four bytes, giving an exponentially larger range.

3. stdlib.h should have something like
#define RAND_MAX 2147483647

Every ANSI/ISO system should have this, and it should be at least 32767

4. If you want values between 0 and 32767, one technique is to compute rand()%32767 to obtain the lower order bits of the value returned by rand(). Another possible approach is to compute rand()/(RAND_MAX/32767 + 1) - which extracts the higher order bits of the result returned by rand(). There are other possible approaches as well.

Note, however, that such approaches can destroy the randomness of the resultant values - depending on how rand() is actually implemented. If you care about how random the values actually are, then test to be sure.