Drupal Caching and hook_init So I was considering using hook_init() to do stuff but I'm not understanding what it means when it says that "This hook is not run on cached pages." Oh really?

So as an experiment I made sure caching was turned on for anonymous users, and went to the site in a browser window where I was not logged in. So I was definitely getting some cached pages, right? Then I added this to my module

function mymodule_init() {
  $num = rand(0,1000);
  drupal_set_message($num); 
}


I figured if the hook wasn't being run, then I shouldn't get a random number each time I viewed the page. I wasn't sure if I should even get a message or not! But I am getting a different random number each time. So what exactly does it mean when it says that the hook isn't being run on cached pages? And when is it NOT ok to use this hook? Sorry if it's something obvious I don't know.

-- edit -- 

Thanks for the answer. I should have realized the drupal_set_message thing was causing the page not to be cached. So I went back and I also tried adding java script like this:

function mymodule_init() {
  $num = rand(0,1000);
  drupal_add_js(array('mymodule' =&gt; array('rand' =&gt; $num)), 'setting');

  $path = drupal_get_path('module', 'mymodule');
  drupal_add_js($path . '/js/mymodule.js');

  echo $num;
}


And in my javascript file:

(function ($) {
    Drupal.behaviors.mymodule = {
        attach: function(context,settings) {
            var num = Drupal.settings.mymodule.rand;

            alert(num);
        }
    }
})(jQuery)


It still seems to alert a random number every time. So is drupal_add_js also disabling the cache? I even tried using a simple echo statement in hook_init() and it still seemed to echo a random number. So are all these things causing the page not to be cached or does hook_init() still get run somehow?
A couple things here:


hook_init() will be run on the first hit on the page, eg, if it's not in cache already, it will be run and then the page will be cached.  But...
A page will not be cached if there are any drupal messages, so, when you call drupal_set_message() it sets drupal_page_is_cacheable(FALSE); and the page isn't cached.


ADDITION:

Regarding your additional code with respect to Javascript:  adding this code in verbatim here does not exhibit the random behavior you see on your site; eg; here the same number appears until the cache is refreshed.

Which made me think about this.  Check your cache times:



If the times are set to &lt;none&gt; I see the behavior you seem to experience, but when they are set to anything else, eg:



then I don't.