Discussion:
[j-nsp] format of minimum and maximum value of math:random() in SLAX
Martin T
2018-07-05 19:41:33 UTC
Permalink
Hi!

According to the documentation, math:random() function returns a
random number with a minimum value of 0 and a maximum value of 1.
Larger values than 0 and smaller values than 1 have a format similar
to 0.663341003779015. What is the format of minimum and maximum value?
Simply 0 and 1? 0.000000000000000 and 1.000000000000000? The reason I
ask is that I use "substring(math:random(), 3, 2) mod 16" for finding
random numbers between 0 and 15 and I need to apply a workaround if
format is not always x.xxxxxxxxxxxxxxx.


thanks,
Martin
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Michael Loftis
2018-07-05 19:55:50 UTC
Permalink
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
Post by Martin T
Hi!
According to the documentation, math:random() function returns a
random number with a minimum value of 0 and a maximum value of 1.
Larger values than 0 and smaller values than 1 have a format similar
to 0.663341003779015. What is the format of minimum and maximum value?
Simply 0 and 1? 0.000000000000000 and 1.000000000000000? The reason I
ask is that I use "substring(math:random(), 3, 2) mod 16" for finding
random numbers between 0 and 15 and I need to apply a workaround if
format is not always x.xxxxxxxxxxxxxxx.
thanks,
Martin
_______________________________________________
https://puck.nether.net/mailman/listinfo/juniper-nsp
--
"Genius might be described as a supreme capacity for getting its possessors
into trouble of all kinds."
-- Samuel Butler
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/
Phil Shafer
2018-07-05 20:38:09 UTC
Permalink
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
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Martin T
2018-07-06 08:42:53 UTC
Permalink
Post by Phil Shafer
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
var $res = floor(rand() * 16);
Also see the "number" statement for additional number-formatting
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
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
2018-07-06 18:47:00 UTC
Permalink
Post by Martin T
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
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp

Loading...