Finally managed to get sampling from cumulative distribution right.