We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20364
    • 89 Posts
    I am sure this is really simple, but can’t get it to work.

    I simply want to have a snippet that returns a random number. Right now the code is just return rand(1,999);

    When called multiple times per page, (even with !), it returns the same number, instead of running back through rand()

    Aka, if the chunk says [[!RandomNumber]] [[!RandomNumber]] [[!RandomNumber]]

    it returns 33, 33, 33.

    Appears to be a caching issue, but don’t know how to get around it.

    Ta!
      • 22303 MODX Staff
      • 10,725 Posts
      Just add a dummy property to each call to make it unique, e.g.
      [[!RandomNumber? &n=`1`]]
      [[!RandomNumber? &n=`2`]]
      [[!RandomNumber? &n=`3`]]
      


      Otherwise, it expects that the Snippet is going to return the same value and is pulling it via it’s tag signature from the request Snippet cache. We realize this is not ideal and that uncached Snippets should not require unique signatures, but for now it is necessary.
        • 20364
        • 89 Posts
        Yeah .. I had tried that, with the same result:

        Snippet RandomNumber:

        <?php
        return rand(1,99999);


        Chunk:
        [[!RandomNumber? &n=`1`]]
        [[!RandomNumber? &n=`2`]]
        


        Result:

        1856
        1856


        ****EDIT****

        I think this has something to do with how I am calling these chunks. Stand by smiley
          • 20364
          • 89 Posts
          Ok, so I was wrong, sort of.

          I am using GetResources to call this Chunk, in two different places on the page. The calls looks like

          [[!getResources? &tpl=`module_tabs` &includeTVs=`1` &processTVs=`1` &resources=`43` &includeContent=`1` &showUnpublished=`1` &showHidden`1`]]


          [[!getResources? &tpl=`module_tabs` &includeTVs=`1` &processTVs=`1` &resources=`44` &includeContent=`1` &showUnpublished=`1` &showHidden`1`]]


          Inside "module_tabs" is where [[RandomNumber]] is called.

          Each time it’s called, it gives back the same number. So it’s not actually two [[RandomNumber]]’s inside one Chunk, it’s two Chunks with one [[RandomNumber]] inside that is giving back the same result.

          Hope that makes sense.

          [Yes I understand it may be strange to have two GetResources calls like this, but it’s the only way I can achieve what I need.
            • 20364
            • 89 Posts
            It appears it’s a similar issue to:

            http://modxcms.com/forums/index.php?topic=57538.0
              • 4172
              • 5,888 Posts
              try
               [[!RandomNumber? &n=`[[+idx]]`]]
              inside of your module_tabs - chunk
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 19568
                • 11 Posts
                [[!RandomNumber? &n=`[[+idx]]`]]


                Just curious, did this work? I am having the same issue. I have multiple calls to getResources on the same page that use the same tpl value.
                  ModX. 'nuff said.