ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Does EE 1 seeds the random generator by itself?

July 29, 2010 7:24am

Subscribe [3]
  • #1 / Jul 29, 2010 7:24am

    Ñuño Martínez

    60 posts

    I have created this plugin:

    class Rnd
      {
        var $return_data = '';
    
    
    
        public function Rnd ()
        {
          global $TMPL;
    
          if (($Min = $TMPL->fetch_param ('min')) === false
          or !is_numeric ($Min))
            $Min = 0;
          if (($Max = $TMPL->fetch_param ('max')) === false
          or !is_numeric ($Max))
            $Max = 1000;
          $this->return_data = mt_rand ($Min, $Max);
        }
      }

    You can use with just “{exp:rnd:rnd min="<min>" max="<max>"}”. Theoretically it should return a different number each time but that’s not true. Each time you reload the page it returns a different number but it returns the same number in the same page. Put some “{exp:rnd:rnd}” in a template and all them returns the same number.

    Why?

  • #2 / Jul 29, 2010 4:44pm

    Ingmar

    29245 posts

    EE treats identical tags as having identical output and uses the cached result for the second and all subsequent times. You’ll have to make sure the tags are not identical, adding as much as a space or other parameter should help.

  • #3 / Jul 30, 2010 5:36am

    Ñuño Martínez

    60 posts

    Thanks Ingmar. I suspeced it would be cached after write the post. So if I use another parameter it should work. I’ll try.

  • #4 / Jul 30, 2010 10:56am

    Ingmar

    29245 posts

    Very good. Let us know how you get on, please.

  • #5 / Oct 05, 2010 5:33am

    Ñuño Martínez

    60 posts

    Finally I’ve used another approach: don’t use an RND plugin. Instead the module has a method to get a list of random values. I mean:

    class Module
        method GetRandomList
        begin
          for n := 1 to MaxValues do
            add element to list
          end for
          return list
        end method
    end class

    As we use this module only once each page, everytime returns a different list of elements.

  • #6 / Oct 05, 2010 10:43am

    Sue Crocker

    26054 posts

    Hi, Ñuño. Thanks for posting your solution. Don’t hesitate to post again as needed.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases