#NEWLINE# def discrete_single_draw( ps , randstate = np . random ) : #NEWLINE# #INDENT# ' Given vector of K positive real weights "ps",\n      draw a single integer assignment in {1,2, ...K}\n      such that Prob(choice=k) = ps[k]\n\n      Args\n      --------\n      ps : K-length numpy vector of positive real numbers\n\n      Returns\n      --------\n      choice : integer in range 0, 1, ... K\n  ' #NEWLINE# totals = np . cumsum ( ps ) #NEWLINE# return np . searchsorted ( totals , ( randstate . rand ( ) * totals [ ( - 1 ) ] ) )