phillipbentonkelly of RAMP - Front End
6/23/2014 - 4:30 PM

Generate a random number between 1- 10 in XSLT

Generate a random number between 1- 10 in XSLT

<!-- Add the 'math' namespace to your XML stylesheet (so we can use 'math:random') -->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:math="http://exslt.org/math"
    extension-element-prefixes="math">
 
<!-- Pick a random number between 1 - 10 and set as a variable -->
<xsl:value-of select="(floor(math:random()*10) mod 10) + 1" />

<!-- In this case math:random()*10 ranges from 0 to 10, with 10 being almost unlikely. 
The trick is to strip it out of the allowed values and then shift the result set ({0, 1, 2, ...}) 
by one position to the right. This is done respectively by mod 10 and + 1. -->

<!-- 
Source: https://gist.github.com/ijy/6614997
-->