TIRand not in range

‘asInteger’ was just an attempt to solve my issue. I removed it.

This is a simplified version of the code.
It is about a “self-triggering” loop player. An initial trigger. The Synth computes a duration. Releases the first loop and starts the second one. The Synth computes a duration. for that one too. Upon release restart the loop 1 with new randomly selected parameters.

// loop 1
trig1=LocalIn.ar(1,0)+Impulse.ar(0); // loop1's trigger is an initial trigger + the release of the loop2

// .. buffer and cue points choice
Poll.ar(poll,elapsed,"*****");
bufidx1=TIRand.ar(0,nbuf-1,trig1).poll(poll,";1_buff index"); // randomly select the buffer index for the loop1
bufnum1=BufRd.ar(1,bBufnum,bufidx1); // bufnum the choosen buffer
ncue1=BufRd.ar(1,bBufnum,nbuf+bufidx1); // # cue points for that buffer
// vvv Those are the ones behaving strangely
cueidx10=TIRand.ar(0,ncue1-2,trig1).poll(poll,";1_cue start");  // randomly select a start cuepoint
cueidx11=TIRand.ar(cueidx10+1,ncue1-1,trig1).poll(poll,";1_cue end"); // randomly select a end cuepoint (after the start cuepoint)
// ^^^ Those are the ones behaving strangely

offset1=Latch.ar(BufRd.ar(1,bBufnum,2*nbuf+bufidx1),trig1); // offset for the buffer in the cuepoint array/buffer
start1=Latch.ar(BufRd.ar(1,cBufnum,offset1+cueidx10),trig1).poll(poll,";1_start"); // real start cuepoint
end1=Latch.ar(BufRd.ar(1,cBufnum,offset1+cueidx11),trig1).poll(poll,";1_end"); // real end cue point

// .. gate
delay1=Latch.ar(((end1-start1)/BufSampleRate.kr(bufnum1)),trig1).poll(poll,";1_delay"); // duration between start and end
rel1=DelayN.ar(trig1,\maxdelaytime.ir(10),delay1); // delay the trigger to get the release trigger
gate1=SetResetFF.ar(trig1,rel1); // build a gate to be used in SelectX

// loop 2
trig2=rel1; // loop2's trigger is loop1's release
// .. buffer and cue points choice
bufidx2=TIRand.ar(0,nbuf-1,trig2).poll(poll,";2_buff index");
bufnum2=BufRd.ar(1,bBufnum,bufidx2);
ncue2=BufRd.ar(1,bBufnum,nbuf+bufidx2);
// vvv Those are the ones behaving strangely
cueidx20=TIRand.ar(0,ncue2-2,trig2).poll(poll,";2_cue start");
cueidx21=TIRand.ar(cueidx20+1,ncue2-1,trig2).poll(poll,";2_cue end");
// ^^^ Those are the ones behaving strangely

offset2=Latch.ar(BufRd.ar(1,bBufnum,2*nbuf+bufidx2),trig2);
start2=Latch.ar(BufRd.ar(1,cBufnum,offset2+cueidx20),trig2).poll(poll,";2_start");
end2=Latch.ar(BufRd.ar(1,cBufnum,offset2+cueidx21),trig2).poll(poll,";2_end");

// .. gate
delay2=Latch.ar((end2-start2)/BufSampleRate.kr(bufnum2),trig2).poll(poll,";2_delay");// /BufRateScale.kr(bufnum2);
rel2=DelayN.ar(trig2,\maxdelaytime.ir,delay2);
gate2=SetResetFF.ar(trig2,rel2);
// gate2=(1-gate1)*EnvGen.kr(Env([0,0,1], [0.1, 0]));

Poll.ar(trig1,delay1, "----TRIG 1-- delay1");
Poll.ar(trig2,delay2, "----TRIG 2-- delay2");

// publish loop2's release, to be used as loop1's trigger in next iteration
LocalOut.ar([rel2]);

From time to time, one of the delays ‘delay1’ and ‘delay2’ gets negative.
The reason is that cueidx10/cueidx11 and/or cueidx20/cueidx20 are out-of-range.
The ‘hi’ limit of the TIRand is 2, and the TIRand outputs 3.
And then it is a cascade. The entire synth starts making non-sense.

Full code is here.