Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getrandmax() why 32767 limitiation on windows?

Tags:

php

random

PHP returns largest possible random value 32767 on windows?

What is constraint on windows?

echo getrandmax(); //32767
like image 358
P K Avatar asked Feb 12 '12 10:02

P K


1 Answers

According to the PHP source-code, getrandmax() is defined as :

PHP_FUNCTION(getrandmax)
{
    if (zend_parse_parameters_none() == FAILURE) {
        return;
    }

    RETURN_LONG(PHP_RAND_MAX);
}

And PHP_RAND_MAX is defined as :

#define PHP_RAND_MAX RAND_MAX

RAND_MAX itself being defined as :

/* System Rand functions */
#ifndef RAND_MAX
#define RAND_MAX (1<<15)
#endif

So, if there is an RAND_MAX defined, it is used...


... And, on Windows with Visual Studio, there is indeed a RAND_MAX defined (quoting) :

The constant RAND_MAX is the maximum value that can be returned by the rand function. RAND_MAX is defined as the value 0x7fff.

So, basically, getrandmax() returns 32767 because that's how it's defined on Windows -- and PHP often uses what the underlying system exports.

like image 155
Pascal MARTIN Avatar answered Nov 03 '22 00:11

Pascal MARTIN