let sample_from_dist dist =
let rec pick d x =
match d with
| [] -> raise (Invalid_argument "sample_from_dist: empty distribution")
| [(_,y)] -> y
| (p,y)::r ->
if p <= x
then y
else pick r (x -. p) in
pick dist (Random.float 1.0)