Proposed RAND() defn

N.B. I'm not sure that a SQL-style definition of RAND(seed) is really practical to define in a SPARQL context without changing a lot of other things.

Though there's a Solution Sequence, there's nothing that requires the SPARQL engine to execute FILTER expressions in any particular order so far as I can tell. We could either drop this feature (not my preference), or relax the wording — if this is an issue. Relaxing the wording would make it hard to test. Thoughts?

- Steve

----

RAND

The RAND function returns an xsd:double in the range [0,1), i.e. 0 ≤ RAND() < 1. The return value may be generated using some stochastic process, or a pseudorandom sequence.

If RAND() is called with no arguments, then it returns a potentially different random/psuedorandom value for each invocation.

If RAND() is called with a numeric argument, then the argument is used as a seed value, returning a consistent value in [0,1) for each solution in the solution sequence for which it is evaluated. Such that, for a given seed RAND(seed) will return the same value whenever it's invoked for evaluation of the first solution in the solution sequence, and a possibly different value consistent value for the second solution, and so on.

RAND(x), where x is a non-numeric type is an error.

Examples:

SELECT (RAND() AS ?r)
WHERE { }
LIMIT 3

?r
--
"0.80498758592176"^^xsd:double
"0.20197435300981"^^xsd:double
"0.59490903801742"^^xsd:double

SELECT (RAND(23) AS ?r)
WHERE { }
LIMIT 3

?r
--
"0.058383725637927"^^xsd:double
"0.18605217354936"^^xsd:double
"0.75510972156665"^^xsd:double

SELECT (RAND(23)+1 AS ?r)
WHERE { }
LIMIT 3

?r
--
"1.058383725637927"^^xsd:double
"1.18605217354936"^^xsd:double
"1.75510972156665"^^xsd:double

Note: the exact values returned will depend on the implementation of the FILTER evaluation software.

Possible test:

data:

<a> <b> 1, 2 .

SELECT ?x
WHERE {
  <a> <b> ?x .
  FILTER(RAND() >= 0)
  FILTER(RAND() < 1)
  FILTER(RAND() != RAND())
  FILTER(RAND(?x) = RAND(?x))
  FILTER(RAND(?x) != RAND(?x+1))
}

Result:

?x
--
1
2

[N.B. It's possible for a compliant implementation to fail this test, as it's not required that RAND(x) != RAND(x+1), or that consecutive invocations of RAND() return different numbers, though it's unlikely to happen in practice.]

-- 
Steve Harris, CTO, Garlik Limited
1-3 Halford Road, Richmond, TW10 6AW, UK
+44 20 8439 8203  http://www.garlik.com/
Registered in England and Wales 535 7233 VAT # 849 0517 11
Registered office: Thames House, Portsmouth Road, Esher, Surrey, KT10 9AD

Received on Monday, 29 November 2010 12:39:34 UTC