1

I have the following line of code in javascript:

(Math.random() + "") * 1000000000000000000

which generates numbers like:

350303159372528000

I tried the same thing in PHP with this:

rand()*1000000000000000000

Which returns:

2.272e+21

I need to use PHP as the number generated will be stored as a SESSION variable and will be used by JavaScript later on.

How do I get PHP to force the number to be an int rather than a float?


EDIT PHP seems to struggle with this.

Would it work if I just generated the rand number in PHP saved it to the SESSION and then done the multiplying by 1000000000000000000 in JavaScript?

How would I go about this?

1321941
  • 2,139
  • 5
  • 26
  • 49
  • 2
    Theres no point on converting to a string and then back to a number again in your Javascript – Paul Jun 13 '12 at 20:08
  • There's a tiny bit more of that line which converts it back into a string, but I did not include it as I didn't think it was relevant. – 1321941 Jun 13 '12 at 20:09

5 Answers5

3

I'd recommend calling

PHP_INT_MAX

To see if your PHP installation can handle an integar that large. I'm guessing it can't which is why it is knocking it down to scientific notation.

WhoaItsAFactorial
  • 3,538
  • 4
  • 28
  • 45
3

I'd suggest converting your result to an int:

intval(rand()*1000000000000000000)

That said, see Kolink and Jeremy1026 answers for precision issues. If you only need an unique identifier, see Truth's answer.

Update: if you're using strings to represent your numbers, don't want or can't use an arbitrary precision library, and don't stricly need perfecly fair random numbers, you could generate smaller numbers and concat them together:

strval(rand()*999999999 + 1) . strval(rand()*1000000000)

(The +1 is to avoid a leading zero in your result; note also that your number will never have a single digit, but every other number is possible)

For a random number with (exactly) 18 digits, you can also use str_pad in the 2nd part, to fill it with leading zeros:

strval(rand(100000000,999999999)) .
    str_pad(strval(rand(0,999999999)), 9, "0", STR_PAD_LEFT)
Community
  • 1
  • 1
mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
  • PHP does not like that and ends up returning 0, seems like MAX_INT issue? – 1321941 Jun 13 '12 at 20:17
  • The docs linked above says: "Strings will most likely return 0 although this depends on the leftmost characters of the string." So it's more likely a string issue (integers would overflow instead AFAIK). See also [this](http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting) (about integer conversion in PHP) – mgibsonbr Jun 13 '12 at 20:20
  • I just tried telling it to be an integer and then evaluating it with intval and it still returns 0, any ideas? – 1321941 Jun 13 '12 at 20:24
  • Have you confirmed that indeed your PHP can handle integers this large? (a 64bit implementation can, a 32bit can't) I can only make further suggestions if you tell more about your requirements. If you really need big numbers, see [this question](http://stackoverflow.com/questions/211345/working-with-large-numbers-in-php), but I'm 99% sure there might be a simpler solution. What exacly will those numbers be used for? – mgibsonbr Jun 13 '12 at 20:35
  • You are a genious! That is such a clever way to get around the issue. Basically, this is for ad generation so I need 18 digit long randomish numbers, it does not have to be fair, just different most times. – 1321941 Jun 13 '12 at 20:50
  • Currently your method is generating too many digits is there any way to make it so it only generates a randomish number with 18 digits? – 1321941 Jun 13 '12 at 21:36
2

If you need a unique identifier (which is what it looks like you're trying to do), please use PHP's uniqid() function.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
2

floor() / ceil() / round() / (int) / intval() will convert the number to int. Also, rand() takes two arguments. If ints are supplied - it will return an integer

And printf() should take care of printing in the format you wish (printf('%d', $int) should do the trick)

karka91
  • 733
  • 1
  • 6
  • 20
  • Also note that [`getrandmax`](http://www.php.net/manual/en/function.getrandmax.php) will tell the greatest value that can be returned (good to know if you're hitting the platform's limit in supported precision). – mgibsonbr Jun 13 '12 at 20:49
  • 1
    The author should have a look at [this thread too](http://stackoverflow.com/q/1479823/1105527) – karka91 Jun 13 '12 at 20:54
1

In the end I solved the issue like this:

<?php
error_reporting(0);
 function RandNumber($e){


 for($i=0;$i<$e;$i++){
 $rand =  $rand .  rand(0, 9);  
 }
 return $rand;

 }

 echo RandNumber(18);
// Outputs a 18 digit random number

?>
1321941
  • 2,139
  • 5
  • 26
  • 49