This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 4844 - [FO] random() function
Summary: [FO] random() function
Alias: None
Product: XPath / XQuery / XSLT
Classification: Unclassified
Component: Functions and Operators 1.0 (show other bugs)
Version: Recommendation
Hardware: PC Windows XP
: P2 enhancement
Target Milestone: ---
Assignee: Michael Kay
QA Contact: Mailing list for public feedback on specs from XSL and XML Query WGs
Depends on:
Reported: 2007-07-10 20:22 UTC by Andrew Eisenberg
Modified: 2008-01-18 20:59 UTC (History)
0 users

See Also:


Description Andrew Eisenberg 2007-07-10 20:22:00 UTC
We have received a request for a random() function [1]. I am entering this bug report on behalf of the message's author.

[1] Random ordering
Comment 1 Andrew Eisenberg 2007-07-10 20:23:12 UTC
The XML Query and XSL WGs discussed this request at our June 27 meeting. We will consider support for this type of non-deterministic function when we begin work on the next version of Functions & Operators.
Comment 2 Andrew Eisenberg 2008-01-18 19:33:34 UTC
While cleaning up some of my email, I noticed that the author of this request later provided an explanation of his need.

He said, in part, 

"Sometimes you want to hide the logic behind the ordering for a list. Without any explicit ordering and depending on how items where added to the database, a resultset could reveal how products differ in price, or freshness. If this is sensitive information, you could sort by a nonsensitive field, like a name or something. But sometimes you want to avoid the strict appearance that comes with alphabetical ordering, or there may not even be any textual data to sort by. Any type of lottery equivalent. Like a competition where highest score is shared by multiple contestants, yet there can be only (n) official winners. Those need to be picked so it doesn't depend on application date, contestant's name etc. So you sort by random and pick the uppermost (n).Similarly, you want to show a selection of 10 out of a larger set. It could be a highlight of products, or a list of online users. Where you don't visitors to say "I've already seen this exact presentation". Anyway, most RDBMS supports random ordering, being standard or not. So it's likely that many developers are already relying on it, and they would expect it to be available in XQuery also, once they make the transition to xml. By the way, if this feature gets adopted, maybe it should be discussed for xsl:sort as well?"
Comment 3 Michael Kay 2008-01-18 20:59:37 UTC
It's worth noting that EXSLT provides a random-sequence() function:

This returns a sequence (of specified length) containing random numbers between zero and 1.

This serves a number of use cases that XSLT users have found useful, for example generating test data. It's probably more general that the requested function to produce a random permutation of a given input sequence; in fact it could be used to underpin the requested function by writing

let $random := random:random-sequence(count($input))
for $item at $p in $inputSequence
order by $random[$p]
return $item