1

The following script provides the following output. I am running PHP Version 5.5.12 with Apache/2.2.15 (CentOS) on i386 server. Note I run the same script on my almost identical server which is x86_64, and I don't experience the error.

<?php
$id=mt_rand ( 1 , 4294967295);
?>

Warning: mt_rand(): max(-1) is smaller than min(1) in /var/www/html/testing/mt_rand.php on line 2

What causes this error, and how do I fix it?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user1032531
  • 24,767
  • 68
  • 217
  • 387

2 Answers2

3

If you want a value between 1 and 4294967295 which is platform independent, use the following:

$id=2147483648+mt_rand(-2147483647,2147483647);

This answers part of the question, but fedorqui answered the real question that an integer on some platforms is limited to +/-2,147,483,647.

user1032531
  • 24,767
  • 68
  • 217
  • 387
2

You happen to use a value that is bigger than the integer maximum. As read in What is the maximum value for a int32?, this value is 2,147,483,647.

From PHP.net in mt_rand():

Description

int mt_rand ( void )

int mt_rand ( int $min , int $max )

Return Values

A random integer value between min (or 0) and max (or mt_getrandmax(), inclusive), or FALSE if max is less than min.

From the PHP manual:

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

Your script fails on 32 bits systems because you are providing a value which is ~4 billion, while the maximum is ~2 billion. A workaround can be to change PHP_INT_MAX.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • not even. that. it's SIGNED 32bit, so ~2billion positive max. – Marc B Aug 27 '14 at 18:38
  • @MarcB sorry I don't understand what you mean. – fedorqui Aug 27 '14 at 18:41
  • I've tried it multiple times, so I am sure I did not just "happen" to hit 4294967295. That being said, I am sure you are right. I'll look into `PHP_INT_MAX`. – user1032531 Aug 27 '14 at 18:42
  • 32bit int has a 4 billion range, 0->2^32. But PHP's are signed ints, so it's -2billion -> +2 billion. OP's code would have failed for **ANY** int >= 2,147,483,648. – Marc B Aug 27 '14 at 18:42
  • @MarcB aaah, true, yes, with anything bigger than that number the output is bad. I will rephrase my answer because I see I did not write it properly. Thanks! – fedorqui Aug 27 '14 at 18:44
  • @user1032531 yes! See http://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-a-int32 for more info. – fedorqui Aug 27 '14 at 18:47
  • `PHP_INT_MAX` is not in either server's php.ini file. Echoing `PHP_INT_MAX` on the server that works shows `9223372036854775807`. The server that doesn't work shows `2147483647`. How do I change it? – user1032531 Aug 27 '14 at 18:48
  • Ah, I see. +/- 2^31 for my i386 server and +/i 2^63 for my x86_64 server. Since I only want a 2^32 unsigned value, I will change my function to `$id=2147483648+mt_rand(-2147483647,2147483647);` – user1032531 Aug 27 '14 at 19:12