EXSLT Random module for EXSLT.NET

| 7 Comments | No TrackBacks

I'm going to implement EXSLT Random module for EXSLT.NET lib. It contains the only extension function:
number+ random:random-sequence(number?, number?) The function returns a sequence of random numbers between 0 and 1 (as text nodes obviously). The first argument is number or random numbers to generate (1 by default) and the second one is a seed.

The problem is that .NET's Random class accepts seed as int, while in XPath numbers are double. So simple (I hope) question: how do you think it should be converted?

Related Blog Posts

No TrackBacks

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/180

7 Comments

The name doesn't matter -- may be rand may be num, ... or whatever.

I will access the individual elements with "*".

Ok, then what elemnt name you suggest?
<random> sounds good to me.

> Sorry, Dimitre, I don't get (uint)
> (4294967295*x) example, could you elaborate it?

Oopps.., I thought the seed was also restricted to the [0,1] interval.

If not, then you must implement the reverse scaling -- from all possible double values to all possible int values.

One way to do this (not scaling, but an operation, which largely preserves the randomness of the seeds) is to find the remainder of dividing the double by the maximum possible int like e.g.:

double d = 1234567890123E0;
int i = (int)(d % 2147483648);


> If I understand correctly, exslt doesn't limit > seed value, it's any double value, right?
> And another question - the function doesn't
> return seed, right?


No, and this is why it only makes sense to ask for a whole sequence of random numbers -- all the rands the app will actually need.

> So, what do you think we should return - text
> nodes or elements?
> Actually many exslt functions (e.g. regexp
> ones) return results as elements, such as . So > we could return nodelist of elements, but the
> problem is exslt page says nothing about the
> name of this element, so that's no t a good
> idea to improvise here. Text nodes are better
> as they don't have names

It actually doesn't matter, as long as the
number() of the returned node(s) has as value the computed random(s).

Anyway, a text node does not exist by itself -- it must have a parent. So you'd be returning text nodes, which belong to a new temporary tree -- you'd have also created the parents of these text nodes. AFAIK the XML Infoset conbines any two adjacent text nodes into a single text node, therefore to avoid this they either will have to have different parents, or be part of mixed content...

It is largely a matter of taste. I'd personally prefer to return element nodes, whose only text node child is one of the generated randoms. This would be convenient in a subsequent xsl:copy-of operation.

Cheers,
Dimitre.

Sorry, Dimitre, I don't get (uint)(4294967295*x) example, could you elaborate it?
If I understand correctly, exslt doesn't limit seed value, it's any double value, right?
And another question - the function doesn't return seed, right?

I've read that in exslt mail list archive.
So, what do you think we should return - text nodes or elements?
Actually many exslt functions (e.g. regexp ones) return results as elements, such as . So we could return nodelist of elements, but the problem is exslt page says nothing about the name of this element, so that's no t a good idea to improvise here. Text nodes are better as they don't have names.

Another comment: When we were specifying the signature of the method, we were all deeply into using XSLT 2.0/XPath 2.0.

This is why we didn't notice that there is no sequence data types in XPath 1.0.

I noticed this later and proposed to the EXSLT-list that the return type must be changed to node* (actually this must be element* ).

This was accepted, however it seems that the web page, which also acts as the spec, has not been updated.


Cheers,
Dimitre.

Try something like this example:

uint i;
double x = 0.1E0;

i = (uint)(4294967295*x);

When you get the random number, you must performe the reverse conversion.

Cheers,

Dimitre Novatchev,

Dimitre Novatchev.
FXSL developer, MVP, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

Leave a comment