We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28173
    • 409 Posts
    Hi,

    I'm looking for a snippet which permit to get a random number inside a range.
    Example to get a random number between 12 and 24: [[RandomNumber?&begin=`12` &end=`24`]]

    I'm newbie with snippet development, and a little help could be usefull smiley
      • 4172
      • 5,888 Posts
      try:

      <?php
      return rand($begin,$end);
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 34084 ☆ A M B ☆
        • 756 Posts
        If you create a snippet with that name RandomNumber and those two parameters the code would look like:

        <?php
        
        if((isset($begin) && isset($end)) && $end >= $begin){
          return rand($begin, $end);
        }
        return 0; // Return zero for false could return something else if desired
        


        Just copy past that into the snippet and also make sure you call the snippet uncached:

        [[!RandomNumber?&begin=`12` &end=`24`]]


        Cheers
          Evo Revo // Ubuntu, CentOS, Win // Apache 2x, Lighttp (Lighty)
          Visit CharlesMx.com for latest news and status updates.
          • 28173
          • 409 Posts
          Thanks you a lot !
            • 52257
            • 11 Posts
            Quote from: charless at Apr 03, 2013, 01:45 PM
            If you create a snippet with that name RandomNumber and those two parameters the code would look like:

            <!--?php
            
            if((isset($begin) && isset($end)) && $end -->= $begin){
              return rand($begin, $end);
            }
            return 0; // Return zero for false could return something else if desired
            


            Just copy past that into the snippet and also make sure you call the snippet uncached:

            [[!RandomNumber?&begin=`12` &end=`24`]]


            Cheers

            Hi,

            I am trying to do the same thing. However, the content get cached even I added ! .

            Any idea?

            Thanks

            Keith