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.
Grow your site’s audience with our AI-driven traffic service, significantly cheaper than paid ad platforms. Contact us to explore the benefits.
https://ow.ly/KJL650XSItJ
WhatsApp business numbers activated, simple setup and instant delivery.
WhatsApp verified numbers for marketing, instant setup and low price packages, easy to scale.
Reddit posting accounts aged and safe, instant supply and fair pricing.
Buy aged Facebook accounts for advertising, low price and instant delivery, ready for campaigns today.
Purchase high quality Reddit accounts, strong karma and aged profiles, delivered quickly and safely.
Buy Facebook aged profiles with friends, affordable and delivered fast.
Unlock more targeted leads with our AI! Watch this short video to get started: https://www.youtube.com/shorts/m26AoaWSFII
Our AI-driven solution delivers keyword and location-specific visitors to your site, offering a budget-friendly alternative to expensive paid advertising. Ready to boost your reach?
marketingager.com
Discover how our AI can skyrocket your Freestyle Developments Co Uk website leads in this quick video: https://www.youtube.com/shorts/d7kGxfGVyNg