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

Martin T m4rtntns at gmail.com
Fri Jul 6 04:42:53 EDT 2018


On Thu, Jul 5, 2018 at 11:37 PM Phil Shafer <phil at juniper.net> wrote:
>
> Michael Loftis writes:
> >idk if there's a floor function but the general solution is floor(rand() *
> >16) when rand() produces values 0-1(exclusive) IE if random does not
> >generate 1.0 - dunno implementation details for slax
>
> Yes, XPath has a floor() function that can be used directly in SLAX.
>
> https://www.w3.org/TR/1999/REC-xpath-19991116/#section-Number-Functions
>
> So you'd say:
>
>     var $res = floor(rand() * 16);
>
> Also see the "number" statement for additional number-formatting
> options:
>
> http://libslax.readthedocs.io/en/latest/content.html#the-number-statement
>
> Thanks,
>  Phil

Michael, Phil,

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://github.com/GNOME/libxslt/blob/master/libexslt/math.c#L465
random number calculation can be "(double)RAND_MAX /
(double)RAND_MAX". So final solution should be "floor(math:random() *
16) mod 16".


Martin


More information about the juniper-nsp mailing list