#608 refactored use of Math.random(), #609 prevent time attacks.

#608 resolved.
#609 resolved.