Fix bernoulli distribution sampling, and add test

Previous version was almost always sampling 1

[issue originally discovered by @thibaudrahier]