question: protected int weightedDistribution ( int [ ] weights ) { int sum = 0 ; for ( int weight : weights ) { sum += weight ; } int val = ( int ) Math . floor ( m_random . nextDouble ( ) * sum ) ; for ( int i = 0 ; i < weights . length ; i ++ ) { val -= weights [ i ] ; if ( val < 0 ) { return i ; } } return - 1 ; }
target:  Calculates a weighted distribution