0

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Cody
  • 5
  • 3
  • I don't think random() exists in php bro. – Cody Jun 16 '15 at 09:25
  • If you had read the documentation of `rand()`, you'd know that it does not return a floating point value from 0.0 to 1.0. The overload for `rand(int, int)` is most likely what you're looking for. But just read the documentation and also please do that first the next time... Asking such trivial questions reduces the overall quality of the site. – foobar Jun 16 '15 at 09:35

6 Answers6

0

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.

dieBeiden
  • 178
  • 6
0

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)
n00dl3
  • 21,213
  • 7
  • 66
  • 76
0

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

Community
  • 1
  • 1
Yinka
  • 26
  • 1
0

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

$rand = rand(3000, 4000);
Alfwed
  • 3,307
  • 2
  • 18
  • 20
0

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.

Vesper
  • 18,599
  • 6
  • 39
  • 61
0

You should use better (performance wise) mt_rand()

var_dump( mt_rand(3000, 4000) );
Community
  • 1
  • 1
viral
  • 3,724
  • 1
  • 18
  • 32