[j-nsp] format of minimum and maximum value of math:random() in SLAX

Phil Shafer phil at juniper.net
Fri Jul 6 14:47:00 EDT 2018


Martin T writes:
>thanks! I didn't know about floor() function in XSLT/SLAX. However,
>looks like in extremely rare cases math:random() can return 1 because
>according to https://...
>random number calculation can be "(double)RAND_MAX /
>(double)RAND_MAX". So final solution should be "floor(math:random() *
>16) mod 16".

Interesting.  The spec says "The math:random function returns a
random number from 0 to 1", which is less than clear about the
inclusivity of the range.  Looks like other implementations (msxml)
call the javascript Math.random() function, which returns [0, 1),
but libxslt returns [0, 1] (inclusive on both ends of the range).

The "mod 16" will give you slightly more 0s than strictly random.
If you care, you might want a custom function, like:

function my:random () {
    var $x = math:random();
    if ($x != 1) {
        result $x;
    } else {
        result my:random();
    }
}

Thanks,
 Phil


More information about the juniper-nsp mailing list