This is one of these very simple programming puzzles I came across recently:

given a function returning random integers from 0 to 4 inclusive with equal probability, write a function returning random integers from 0 to 6 inclusive.

Of course the solution should also return equally distributed numbers. So let’s start from an input function sample definition:

def rand4() = (math.random * 5).toInt

Your task is to implement
rand6() by only using
rand4(). Give yourself few minutes and continue reading.

.

.

.

The first approach is pretty straightforward but happens to be completely broken:

def rand6() = rand4() * 3 / 2

As simple as that. In ideal solution each output value from
0 to
6 should appear with the probability of
1/7. Can you tell from the code above, what's the probability of
rand6() returning
2 or
5? That's right, it's no more than
0, you'll never get these values. I hope it's clear why. So let's go for something more sophisticated:

def rand6() = (rand4() + rand4()) % 7

Looks better, but still pretty far. The code above has two major flaws. First of all the results of
rand4() + rand4() expression range from
0 to
8 but we need
0 to
6. The obvious solution is to use
% 7 operation. However this results in
0 and
1 being returned twice as often because
7 and
8 are overflowing to
0 and
1. So what about this:

I hope the recursion (which can easily be turned into loop, but I leave that work to the Scala compiler) is not obscuring the intent - if the sum of two
rand4() invocations is above expected result, we simply discard it and call
rand6() again. However there is still one subtle but striking bug, quoting Wikipedia on
uniform distribution

This program simply places pixels at
random(X, Y) positions on each panel. In the first panel I use one
Math.random() * 300 call scaled to fit whole canvas. As you can see the distribution is more or less uniform. But we can't tell that about second and third panels. On the second panel I am using the sum of two uniformly distributed variables, in principle:
(Math.random() + Math.random()) * 150). Even though this expression can return anything between
0 and
300, the points are very biased toward the middle of the canvas (triangular distribution). The same behaviour is emphasized on the third panel where ten invocations of
Math.random() are used.

The correct answer

The approach I'm taking is based on the observation that
rand4() is capable of producing two random least significant bits. So let's start from implementing
rand3() with known semantics:

def rand3(): Int = rand4() match {
case 4 => rand3()
case x => x
}

rand3() returns uniformly distributed values from
0 to
3 doing so by rejecting
4 output of
rand4(). How will that help us? Well, we now have two random bits, each one being either
0 or
1 with 50% probability. We can easily widen it for larger sequences, e.g.
rand15() and
rand7():

def rand15() = (rand3() << 2) + rand3()
def rand7() = rand15() >> 1

You should be rather comfortable with the bit fiddling above. Having the ability to produce two random bits I can easily generate 4 and 3. Now
rand6() is a no-brainer:

def rand6() = rand7() match {
case 7 => rand6()
case x => x
}

Just to make this lesson a little bit more interesting, let's implement
randN(n: Int) on top of
rand4().
randN() should return uniformly distributed natural values from
0 to
n. I'll begin by implementing helper method
atLeastKrandBits(k: Int) returning...
at least K random bits:

Conclusions

You might ask yourself a question:
why should I even care? If you fail to understand probability distribution your application might produce random output that's actually quite easy to predict. It's not a big deal if you are writing a game and enemies are more likely to appear at some places on the map (but the players
will discover and abuse it!) But if you need random numbers for security reasons or you rely on uniform distribution e.g. for load-balancing purposes - any bias might become fatal.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.