double qobs ( int dummy , double [ ] xt ) { int x = ( int ) ( xt [ 0 ] ) ; double p = Math . random ( ) ; double [ ] distr = accumP [ x ] ; int i , left = 0 , right = distr . length - 1 ; // Find and return least i s.t. p <= distr[i] while ( left <= right ) { // Here distr[left-1] <= p <= distr[right+1] i = ( left + right ) / 2 ; if ( p < distr [ i ] ) { right = i - 1 ; } else if ( distr [ i ] < p ) { left = i + 1 ; } else { return i ; } } // Now p <= distr[left] return left ; }
An efficient version of qobs using binary search in accumP