protected int randomIntInWindow ( int begin , int windowSize , int n , Set < Integer > excludeSet ) { final int size = Math . min ( windowSize , n ) ; if ( size <= 0 ) { return - 1 ; } int adjustment = 0 ; for ( Integer v : excludeSet ) { int vindex = ( v . intValue ( ) - begin + n ) % n ; if ( vindex < size ) { adjustment ++ ; // calculates excluded elements within window } } if ( adjustment >= size ) { return - 1 ; } int rindex = r . nextInt ( size - adjustment ) ; // ith element is chosen int iterator = begin ; for ( int i = 0 ; i <= rindex ; i ++ ) { while ( excludeSet . contains ( iterator ) ) { iterator = ( iterator + 1 ) % n ; } if ( i != rindex ) { iterator = ( iterator + 1 ) % n ; } } return iterator ; }
returns a random integer within a modular window taking into consideration a sorted list of nodes to be excluded .