Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is RAND_MAX guranteed to be <= UINT_MAX?

Tags:

c

c99

I'm trying to determine if RAND_MAX can fit inside an unsigned int variable. After looking through the C99 standard, I have only found that RAND_MAX is guaranteed to have a value of at least 32767. While I know that RAND_MAX expands to int value, I'm not sure if it's a long int. Right now I'm working with unsigned long int variables, but I would like to simplify my source code to use unsigned int variables.

To summarize, is this assumption statement true for the C99 standard:

RAND_MAX <= UINT_MAX <= ULONG_MAX

Also, is RAND_MAX is explicitly less than (i.e. not equal to) UINT_MAX or ULONG_MAX. I realize that the value of RAND_MAX is often a Mersenne prime, but I'm not sure if this is a standard.

like image 879
Vilhelm Gray Avatar asked Dec 17 '25 07:12

Vilhelm Gray


1 Answers

RAND_MAX is the maximum value that can be returned by rand(). Since rand() is defined as returning int, RAND_MAX will be no more than INT_MAX, and therefore also no more than UINT_MAX.

like image 60
interjay Avatar answered Dec 20 '25 01:12

interjay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!