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.

225 thoughts on “Simple FreeMarker random number generator

  1. Is your Freestyle Developments website maximizing its reach? Many websites lose thousands of visitors daily due to lack of exposure. With our AI-powered traffic solution, you can reach a much larger audience.

    To show you the impact, we’re offering a free trial that delivers four thousand highly targeted visitors to your website. See the results firsthand, and then scale up to 400,000 visitors per month with our advanced packages. Let’s supercharge your website’s traffic and turn visitors into opportunities. Get more info here: https://cutt.ly/NrcJztOI

  2. Is your website freestyle-developments.co.uk missing out on its true potential? With our AI-driven traffic system, you could connect with thousands of additional visitors daily—without any extra effort on your part.

    Take advantage of our complimentary offer that delivers four thousand targeted visitors so you can test the impact. If you love the results, our plans offer up to 350K visitors per month. Let’s turn missed opportunities into growth. Get more details here: https://cutt.ly/PrcLzLGQ

  3. Use AI to build a website by just entering a few lines of text for free. Our AI powered system will then build an entire website in just a few minutes. Try it free here: https://ow.ly/bml650VqFCm

  4. I do not eve know how I ended uup here, bbut I thought this pokst wwas good.
    I don’t know who you are but certainly yyou arre goin to a famus blogger iif yyou arre not already ;) Cheers!

  5. Attract more leads for your freestyle-developments.co.uk website by leveraging AI on Instagram. If you’re looking to increase enhanced traffic, generate leads, and grow your brand’s reach, you can get more information and start a complimentary trial here: https://ow.ly/XBrW50VycRS

    This is an AI-powered Instagram growth service that:
    -Grows followers with focused, premium audiences.
    -Enhances engagement through advanced AI algorithms.
    -Targets users based on hashtags and accounts they follow.
    -Reduces effort by automating tedious Instagram tasks.

    Our service prioritizes on real, organic growth—zero bots, without fake followers. It’s ideal for brands like yours that want to transform Instagram into a lead generation engine. Better yet, our service is provided on a month-by-month subscription basis so you can stop at any point you like. No contracts and a 7 day free trial.

  6. What if your Freestyle Developments website experienced a huge surge in traffic? Many businesses fail to attract thousands of daily visitors simply because they don’t have enough exposure.

    With our cutting-edge traffic solution, you can get four thousand highly targeted visitors as part of a free trial. If you like what you see, our plans scale to 400K visitors monthly—helping your site grow. Get more details here: https://cutt.ly/6rbn62vv

  7. Is your website freestyle-developments.co.uk overlooking its true potential? With our intelligent traffic system, you can reach thousands of additional visitors daily—without any extra effort on your part.

    Take advantage of our complimentary offer that delivers four thousand targeted visitors so you can experience the impact. If you love the results, our plans scale up to 350,000 visitors per month. Let’s convert lost traffic into growth. Get more details here: https://cutt.ly/3rbmwGFS

  8. Your website, freestyle-developments.co.uk, has the potential to reach far more visitors than it currently does. Many sites overlook an enormous audience. That’s where our AI-powered traffic system transforms results—delivering relevant visitors straight to your site.

    Start with a complimentary 4,000-visitor boost to see the benefits. Then, scale with packages offering up to 350K visitors monthly. Let’s accelerate your traffic today. Get more info here: https://cutt.ly/ErbmwDvq

  9. Capture additional leads for your freestyle-developments.co.uk website by leveraging AI on Instagram. If you’re looking to increase more traffic, generate leads, and grow your brand’s reach, you can find more information and start a complimentary trial here: https://ow.ly/FW1p50VycSn

    This is an AI-powered Instagram growth service that:
    -Grows followers with focused, top-tier audiences.
    -Improves engagement through intelligent AI algorithms.
    -Focuses on users based on hashtags and accounts they follow.
    -Eliminates effort by automating repetitive Instagram tasks.

    Our service prioritizes on genuine, organic growth—no bots, zero fake followers. It’s perfect for brands like yours that want to turn Instagram into a lead generation engine. Best of all, our service is provided on a month-by-month subscription basis so you can cancel whenever you like. No contracts and a 7 day free trial.

  10. Wanting to enhance your website audience? Our AI-enhanced brings tailored website traffic using keywords or place-based tags from continents to city blocks.
    Aiming for more revenue, active pages, or greater digital reach?
    We adjust it to your goals. Enjoy a 7-day free trial period with no contract. Begin here:

    https://cutt.ly/rrQWZNO3

  11. Every day, websites like freestyle-developments.co.uk lose valuable traffic opportunities. Don’t let yours be one of them. Our AI-powered traffic system is designed to boost visibility and bring highly targeted traffic to your site.

    Claim your 4,000 visitor test run to experience the benefits firsthand. Then, scale up to plans offering up to 350,000 visitors per month. It’s time to realize your website’s true traffic potential. Get started here: https://cutt.ly/arQWZOZj

  12. Get more leads for your freestyle-developments.co.uk website by using AI on Instagram. If you’re looking to increase enhanced traffic, create leads, and expand your brand’s reach, you can find more information and start a free trial here: https://ow.ly/aSLa50VycS2

    This is an AI-powered Instagram growth service that:
    -Increases followers with focused, high-quality audiences.
    -Improves engagement through smart AI algorithms.
    -Aims at users based on hashtags and accounts they follow.
    -Reduces work by automating tedious Instagram tasks.

    Our service focuses on authentic, organic growth—zero bots, zero fake followers. It’s excellent for brands like yours that want to convert Instagram into a lead generation powerhouse. Even better, our service is provided on a month-by-month subscription basis so you can opt out any time you like. No contracts and a 7-day complimentary trial.

  13. Wanting to tap into fresh website traffic? Our AI-enhanced delivers ideal visitors using keywords or geographic targeting from continents to towns.
    Desiring higher income, livelier users, or greater digital reach?
    We fine-tune it to your plan. Enjoy a 7-day free trial period with no contract. Start now:

    https://cutt.ly/mrQWZMPb

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>