Volatile functions

7.87K viewsScripting

I’m writing a plugin function which is volatile – repeated calls to it return varying values – but I’m having trouble making this work.

As an example, consider the built-in rand() function. This returns a different value after each call (i.e. it has type “”IO double”” rather than “”double””, to borrow the Haskell convention).

Now if I write a Groovy function:

double groovy_rand() {
return new Random().nextDouble();

Then this behaves as I would expect, in that a 2D matrix with e.g. A1 = groovy_rand() produces a list of random numbers across category B.

But if I write a plugin function:

public class PluginRand extends AbstractQFunction {

public QValueList evaluate(QValueList[] args) {
return QValueFactory.createSingletonValueList(new Random().nextDouble());

public QType[] argumentTypes() {
return new QType[] {};

public QType returnType() {
return QType.cValueType;

then doing the same A1 = pluginrand() just produces a list across B of the same *first* random number.

How can I obtain the Groovy-like volatile behaviour from a plugin?

Many thanks.


Yes, the reason for this behavior is this function:

[code:3nysy3ps] QValueFactory.createSingletonValueList(getRandomNumber()) [/code:3nysy3ps]

createSingletonValueList() creates a list with just one single (-> singleton) value.

Is there another function in the QAPI who allows creating a list with different values? Or is it possible to make this function volatile?


You are viewing 1 out of 8 answers, click here to view all answers.

Latest Questions