### Nicholas Vanderweit

Shared publicly -While writing a simulation in Scala, I ran into an interesting programming exercise that was fun to implement in a functional style. I'd be interested to see how other people solve this.

Given an outcome of a uniform random variable X ~ U(0,1) and the probability mass function of a discrete random variable Y, compute a random outcome of Y.

So, you have random numbers in [0, 1), given to you by your standard library, and a mapping like ("heads" -> 1/3, "tails" -> 2/3), and you want to come up with a function to give you "heads" or "tails" with the right probabilities.

Here's my solution (in Haskell):

https://gist.github.com/nvanderw/5269152

+Ken Sheedlo +Sam Pottinger

Given an outcome of a uniform random variable X ~ U(0,1) and the probability mass function of a discrete random variable Y, compute a random outcome of Y.

So, you have random numbers in [0, 1), given to you by your standard library, and a mapping like ("heads" -> 1/3, "tails" -> 2/3), and you want to come up with a function to give you "heads" or "tails" with the right probabilities.

Here's my solution (in Haskell):

https://gist.github.com/nvanderw/5269152

+Ken Sheedlo +Sam Pottinger

1

Add a comment...