Why would rand() return a negative value when min and max values are positive?

后端 未结 2 1943
南笙
南笙 2020-12-20 01:34

I have a simple piece of PHP code which requires a random number to be created. However, even though the input is always positive, it sometimes returns a negative output.

2条回答
  •  無奈伤痛
    2020-12-20 01:49

    Rand takes as arguments, and returns, integers. Integers (in PHP) usually have a maximum range of 2**32.

    Your double arguments are larger than this, causing an integer overflow when they are converted to integers.

提交回复
热议问题