Noob Coder
Noob Coder

Reputation: 369

PHP mt_rand return same number every time

random.php

$min=1;
$max=13031;
$ran_num = mt_rand($min,$max);
echo $ran_num;

This is working fine on my local host, but when I run it on my server, it always return the same number. not sure what is going on.

Update: I am running a wordpress site, and I put the random.php in the theme folder. I also have eaccelerator installed, this might be the issue, I am looking into it now

Upvotes: 2

Views: 2093

Answers (3)

Tomas M
Tomas M

Reputation: 7343

Wordpress may use cache, and if you reload the page, it loads from cache, so you will see the same random number again and again. It has nothing to do with PHP version in this particular case - to fix this, it is needed to disable the wordpress cache.

Upvotes: 0

kenorb
kenorb

Reputation: 166429

By default modern PHP creates different number for mt_rand (it's uses libc random number generator), so the problem can be somewhere else.

This can likely to be caused by some caching, for example check the followings:

  • clear your web browser caching (use private mode or use different browser),
  • disable Varnish (if it's in use),
  • check whether code is using memory caching like Memcached/Redis,
  • this may be related to PHP cache accelerators (but less likely),
  • restart your web server just in case (could be some extra caching module),
  • make sure you're editing the right file and it was deployed properly,
  • test it in the smallest possible PHP code.

Upvotes: 0

Iłya Bursov
Iłya Bursov

Reputation: 24146

If your server has PHP lower than 4.2 - you need to call to mt_srand before you can use mt_rand

Upvotes: 2

Related Questions