Search code examples
randomcomplex-event-processingesperepl

Return random event out of last 2 events


Assume the following Esper events (Esper Tryout page):

StockTick={symbol='event1', price=1}
t=t.plus(10 seconds)
StockTick={symbol='event2', price=2}
t=t.plus(10 seconds)
StockTick={symbol='event3', price=3}
t=t.plus(10 seconds)
StockTick={symbol='event4', price=4}

I would like to return a random out of the last two events every 3 seconds.

I started with the following try but it does not work because the random variable never changes once it is initialized.

create schema StockTick(symbol string, price double);
create variable int size = 2;
create variable int rand = cast(Math.random()*size, int)+1;
@Name('Out') 
select prevtail(price) from StockTick#length(rand) output every 3 seconds;

I am grateful about any ideas.


Solution

  • I could think of a bunch of possible ways.