为什么这个PHP数学代码不起作用

I am trying to convert this as3 code to php but its not working correctly. I need it like the as3 one generating thanks!

PHP:

print(floor(rand() * 1000) + 3000);

Result:28240000

AS3:

var intCID = Math.floor(Math.random() * 1000) + 3000;
var strSessionId = String(intCID);
trace(strSessionId);

Result:3330

You are calling rand() without argument, and according to docs:

if called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax()

so you should use this instead :

rand(0,1000)

PHP's rand() function gives you a random integer value, not a float (decimal) value. Therefore you get this real big value if you do * 1000

Be sure to check the PHP manual's rand() function. You will see that you can set the min and max value for generated random numbers, like rand(1, 1000)

Hope this help generating your intended values.

Description

int rand ( void )

int rand ( int $min , int $max )

If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 5 and 15 >(inclusive), for example, use rand(5, 15).

Source: http://php.net/manual/en/function.rand.php

Use: rand(0,1) to get the same result

Your AS3 code generate a random number between 3000 and 4000 so the equivalent in PHP would be :

$rand = rand(3000, 4000);

In PHP, rand() returns a value from 0 to getRandMax() which is often 32767, and is integer, so the call to PHP rand() should be emulated as this AS3 code:

Math.floor(Math.random()*32768) // returns 0..32767 as integer

The PHP code rand($min,$max) is emulated by the following:

Math.floor(Math.random()*(1+max-min))+min

But, by all means do NOT expect that both PHP and AS3 code will return the exact same value.

You should use better (performance wise) mt_rand()

var_dump( mt_rand(3000, 4000) );