Re: [xml] EXSLT random() function

On Tue, Feb 19, 2008 at 12:06:38PM -0600, Chuck Bearden wrote:
Should I expect the results of the EXSLT random() function to be 
different for each invocation?  I transform an XML file with an identity 
transform, changing it only by adding an attribute @tyche with the value 
of the EXSLT random function.  For a given input file and a given 
stylesheet, the output of all transformations are identical.  Is this 
expected?  My hope was to use random() to make a random selection of 
elements from a document, such that each time I made the selection I 
would get a different selection.

I'm posting sample input, stylesheet, and output files at
'randomize.xsl' is the stylesheet.  'modules.xml' is the input. 'one' 
and 'two' are the output from two invocations of the transformation as 
'xsltproc randomize.xsl modules.xml'.

$ xsltproc --version
Using libxml 20627, libxslt 10120 and libexslt 813
xsltproc was compiled against libxml 20627, libxslt 10120 and libexslt 813
libxslt 10120 was compiled against libxml 20627
libexslt 813 was compiled against libxml 20627

 Use the source, it's available !

static double
exsltMathRandom (void) {
    double ret;
    int num;

    num = rand();
    ret = (double)num / (double)RAND_MAX;

and man rand() states

       The  rand()  function  returns  a  pseudo-random  integer between 0 and

       The srand() function sets its argument as the seed for a  new  sequence
       of  pseudo-random  integers  to be returned by rand().  These sequences
       are repeatable by calling srand() with the same seed value.

       If no seed value is provided,  the  rand()  function  is  automatically
       seeded with a value of 1.

I don't see any srand() call in that module.
So yes I would expect the behaviour of exslt:random to be predictable and

just states 
  'The math:random function returns a random number from 0 to 1.'

so apparently libexslt implementation might just be conformant, but
as usual we takes patches ... But I don't see how you would seed in
a truely random fashion while keeping the code portable.

I would actually think that if you link to libxslt, and use it to
process multiple document with that stylesheet in the same process, you
would get the expect effect, just that a single xsltproc instance will
always return the same on the same system.


Red Hat Virtualization group
Daniel Veillard      | virtualization library
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]