Re: [F&O] LQ-FO-001 - random numbers

> Many languages provide a random number generator.  This can
> be useful for testing; in my cases I wanted it as part of
> generating SVG images.
> 
> Although it's possible to write a random number generator in
> XQuery, it's hard to do with any efficiency, because a random()
> function will always return the same random number (e.g. there's
> no assignment to global or hidden variables).

A set of useful random functions have been implemented (and are in use for
almost two years) in pure XSLT 1.0 as part of FXSL -- see e.g.
http://fxsl.sourceforge.net/articles/Random/Casting%20the%20Dice%20with%20FXSL-htm.htm

> 
> Two obvious workarounds are (1) an external function, which isn't
> always possible and isn't likely to be interoperable, and (2)
> writing a function random(n) which returns the nth random
> number in a series; this can then moderately easily be written
> as a recursive function, with some care to avoid overflow.
> 
> I'd like to suggest either
> (1) adding a random() function, possibly with an integer argument
>     as outlined above, or,

The function thus defined will have the following drawbacks:

  1. It will always generate the same sequence of random numbers

  2. Its efficiency when used to get a sequence of random numbers will be 
O(N^2) (if it must not have side effects)

> 
> (2) defining (in the future) a set of commonly needed external
>     functions, and possibly adding a way to indicate that an
>     external function may return different values on multiple
>     invocations.

  This is being done by EXSLT.org.

  Many useful functions, e.g. trigonometric, exponential, etc. have been
implemented in the FXSL library, in pure XSLT 1.0 and XSLT 2.0.

Best regards,

Dimitre Novatchev.



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com

Received on Thursday, 4 March 2004 17:09:46 UTC