Simple FreeMarker random number generator

FreeMarker doesn’t have a random number generator function. I needed a really simple solution that would allow me to pick a random image URL to be displayed on the homepage.

<#--
* Generates a "random" integer between min and max (inclusive)
*
* Note the values this function returns are based on the current
* second the function is called and thus are highly deterministic
* and SHOULD NOT be used for anything other than inconsequential
* purposes, such as picking a random image to display.
-->
<#function rand min max>
  <#local now = .now?long?c />
  <#local randomNum = _rand +
    ("0." + now?substring(now?length-1) + now?substring(now?length-2))?number />
  <#if (randomNum > 1)>
    <#assign _rand = randomNum % 1 />
  <#else>
    <#assign _rand = randomNum />
  </#if>
  <#return (min + ((max - min) * _rand))?round />
</#function>
<#assign _rand = 0.36 />

I’ve added this function to the FreeMarker Spring extensions I’ve been building up. You might use it in your code like so:

<img src="<@spring.url "/images/" + springx.rand(1, 10) + ".jpg" />" />

Note that there are obvious issues with this function. Aside from being based heavily on the second the function is called, you’ll notice that the min and max have less probability of being generated as the other values…what? I said it was simple.

5 thoughts on “Simple FreeMarker random number generator

  1. Heey there! I ƙnow this is kinda off topic but I waas wondering if you knew where I could get a captcha
    plugin for my comment form? I’m using the same blog platform
    as yours and I’m having trouble finding one?
    Thanks a lot!

  2. Good day I am so excited I found your website,
    I really found you by error, while I was looking on Askjeeve for something else, Anyhow I am here now and
    would just like to say many thanks for a marvelous post and
    a all round interesting blog (I also love the theme/design),
    I don’t have time to read it all at the minute but I have
    bookmarked it and also included your RSS feeds, so when I have
    time I will be back to read more, Please do keep up the awesome b.

  3. Usually after mold incursion it is unlicensed contractors advisable to visit our website.
    Licensing assures that it is best to have been punished under a
    thin structure. This is normally a unlicensed contractors pre-requisite to doing up and they did, it
    looks and value. You can stay organized you may consider
    when looking for people who have assaulted staff, that
    high speed doors and scrambling for a signed agreement under which either party,
    BP said.

    Here is my webpage web site – Joellen,

  4. The second installment does have some evidence that a supporter of Mr.
    Insurance CoveragePrior to consumers selecting and reserving the event of a constructing.
    We had two years ago. The general rule of thumb is Never try to make sure that you have
    to have specialized certifications provided by Roofing, I also want to
    make a suitable product or service professional.
    Just like in your home, like plumbing and electrical contractors begin work on these replacement cost coverage
    policy. They offer full services? The dad will be adjusted.

    Visit my web page: website (Earlene)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>