As a preparation for the selection of the event we sum the values in the rate array:
for (is = 1; is < last; is++)
queue[is].rate += queue[is - 1].rate;
sum = queue[last - 1].rate;
dt = -log(ran(0)) / sum;
sum is the sum in equation \ref{pn}
queue[n].rate is $\sum_{i=0}^{n} r_i$
dt is the timestep, equation \ref{tau}.
If we scanned all sites and found no possible events, nothing can happen. At first sight this may seem silly, however, it does occur and must be handled correctly:
if (last == 0)
return;
We select a random number, level, between 0 and sum, search for the corresponding entry in queue, and decode and execute the action represented by this entry:
if (tsum + dt * ran(0) <= tstep)
{
level = sum * ran(0);
for (is = 0; is < last; is++)
if (level <= queue[is].rate)
break;
if (queue[is].action < NBRNUM)
swapAtm(queue[is].target, queue[is].action);
else
desTwo(queue[is].target, queue[is].action - NBRNUM);
}