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.

377 thoughts on “Simple FreeMarker random number generator

  1. Drive high-quality traffic to your website with our AI-powered solution, far more affordable than expensive paid ad campaigns. Ready to scale your site?

    https://ow.ly/68p650XGmTK

  2. Save on marketing with our AI-optimized service, delivering keyword and location-targeted traffic for less than paid ad campaigns. Start scaling your website now.

    https://marketingager.com/

  3. Our AI-powered service brings precise, keyword-driven traffic from your chosen locations to your website, all at a fraction of the cost of paid ads. Connect with us today.

    https://marketingager.com/

  4. Hi,
    freestyle-developments.co.uk is loading slower than 80% of sites on mobile — Google says that’s silently killing ~40% of visitors (and sales) before they convert.
    We ran a free 30-second audit and it shows solid room for 40%+ faster loads.
    See your exact Google score + revenue impact here (pre-loaded):
    https://aerotraffic.com/speed-test/?url=https://freestyle-developments.co.uk
    Takes 20 seconds — no obligation.

  5. Unlike other marketplaces, these exchanges focus
    on limited-release and high-priced works. Selling digital assets can be complex, especially when you try to vend something you’ve made yourself.
    Locate the “Create” button in the top right corner of
    most platforms to begin minting your NFTs.

    This option is normally in the top right-hand corner of most NFT marketplaces’ homepages.
    By selling NFTs using the NFT marketplace, the original creator becomes
    entitled to a specific percentage of future sales
    of the artwork.
    Magic Eden’s NFT assets are expanding by the day, and this growth will continue,
    resulting in higher trade activity and a robust and flexible
    community. The best site to trade or exchange Punks is at Larva Labs, which is the principal
    market for these assets. Deposit your Ethereum into the MetaMask wallet app after you’ve successfully registered.
    Keep your seed phrase safe and never share it with anyone.
    The game is a digital pet world in the sense that it focuses on imagined
    creatures.
    Make sure to think ahead and develop one capable of handling your
    user base efficiently! Utilizing features such as peer-to-peer transfers
    further strengthens the convenience factor of
    the wallet. Before deciding if a platform suits your purposes, perusing community feedback regarding dependability is wise.
    You’ll also want to check what data protection measures the NFT marketplace adopts, such as encryption protocols and mechanisms
    for addressing disputes. If someone bids for it, all that’s left is transferring ownership from buyer to seller via the platform.
    To begin with, you’ll need to upload your item onto the marketplace
    and choose between selling it at a fixed rate or an auction.

    References:
    https://blackcoin.co/explore-the-worlds-biggest-source-of-information-about-online-casinos/

  6. Welcome to the most comprehensive, unique list of game
    hacking resources on the web! These links lead to reliable sources where you can play a wide variety
    of games without encountering network restrictions.

    Below is a curated list of backup links to unblocked game websites.
    A repository with more than 400 popular Roblox games
    with their source code.
    A classic arcade game where you control a spaceship and try to defend against
    waves of invading enemies. A single-player game where the player controls a snake
    that grows each time it eats food. The game provides hints after each guess, indicating if
    the target number is “Higher” or “Lower” than the player’s guess.
    A number guessing game where the player tries to guess a randomly generated number within a
    range. Each game is contained within its own folder, complete with the main game code,
    unit tests, and detailed instructions for playing.
    There was an error while loading. Flip the cards, try to remember their
    positions, and match all pairs to win the game! A puzzle game where the objective
    is to combine tiles with the same value to reach the elusive 2048 tile.

    References:
    https://blackcoin.co/live-casino-hotel-maryland-the-ultimate-online-gambling-guide/

  7. Its unique features, plus the iconic hair curler building design of the Wrest Point Casino
    Tower, set it apart from every other hotel in Australia.
    It was a pioneering entertainment venue, brimming with sophisticated drinking, dining and
    show options that were unprecedented in the country.
    Make sure to present your rewards card every time you dine, gamble,
    or stay in a hotel to enjoy all of the benefits available.

    You’ll find easy-going lunches and snacks to hold you over until supper at either of these locations.

    It’s simple to treat yourself to nine wonderful eateries and hotel accommodations.
    Whether you decide to stay at the Motor Inn or in the Tower
    Premier Suite, you’ll find that your hotel accommodations
    at Wrest Point are some of the best around. Located in Sandy Bay, this luxury hotel is within 2 mi (3 km) of Salamanca Place,
    Salamanca Market, and Constitution Dock. For those seeking proximity to the water, the Water Edge rooms provide serene river
    views, while the Motor Inn rooms offer comfortable and affordable options with easy access to the resort’s facilities.
    Wrest Point’s location, stunning views and spaces that include foyers, restaurants,
    bars, and large and small private rooms ensure
    every occasion will impress.

    References:
    https://blackcoin.co/allslots-casino-poker-no-deposit-bonus/

  8. You also get access to crypto deposits, generous bonuses, and professional support around the clock.If you’re looking to play at a legit, real money online casino in Australia, Level Up
    is a smart and safe choice. Level Up Casino checks a lot
    of boxes for Aussie players — it’s mobile-friendly, crypto-ready, and full of popular games from trusted providers.Now that you
    know the basics, let’s move on and take a closer look at some
    of the bonuses and promotions waiting for new players. Here’s a full breakdown of what Level Up
    Casino offers for players in Australia — from licenses and devices to
    payment options, withdrawal limits, and game providers.

    Level Up Casino implements multiple layers of
    security to protect player information and ensure fair gaming.

    While telephone support isn’t currently available, the existing channels provide comprehensive coverage for player
    needs. Level Up Casino prioritizes player support and security through comprehensive systems
    designed to address player needs and protect sensitive information. This dual
    approach ensures that all players can find a payment method that suits their preferences and circumstances.

    Level Up Casino’s banking infrastructure reflects modern player preferences by supporting
    both traditional payment methods and cutting-edge cryptocurrency options.
    The program also includes CP-to-cash conversion systems,
    higher withdrawal limits, and access to personal account managers for
    premium support.
    No casino is perfect, and such is the case of LevelUp Casino Australia.
    LevelUp has a few years of experience under its belt, having been launched
    in 2020. It operates numerous popular casino brands, most of which
    are licensed by the government of Curaçao.

    References:
    https://blackcoin.co/yabby-casino-discover-top-online-gaming-in-australia/

  9. Get more clicks for less with our AI-optimized traffic service, offering targeted visitors at a lower cost than paid advertising. Grow your site now.

    https://ow.ly/7Iqw50XO1ln

  10. Get more clicks for less with our AI-optimized traffic service, offering targeted visitors at a lower cost than paid advertising. Grow your site now.

    https://marketingaged.com/

  11. Grow your site with our AI-driven traffic solution, delivering keyword and location-targeted visitors at a lower cost than pricey paid advertising. Get started now.

    https://ow.ly/e8ef50XO1lK

  12. Increase your website’s audience with our AI-driven service, offering keyword and location-targeted traffic at a significantly lower cost than paid advertising. Ready to grow?

    https://ow.ly/VQ5L50XO1lN

  13. Attract high-quality visitors to your site with our AI-driven traffic solution, far more affordable than traditional paid advertising. Ready to boost your conversions?

    https://marketingager.com/

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>