Immune System - B-cell receptors
This is an excellent question. You are on the right track with the random match idea. B-cells (as a group) develop receptors that bind every possible antigen and then get rid of the receptors that match antigens in our body. Some textbooks are a little more careful in their language, though, and say almost every possible antigen.

See Alberts


  The preimmune repertoire is apparently large enough to ensure that there will be an antigen-binding site to fit almost any potential antigenic determinant


I'd note, though, that we've yet to identify an antigen that can't be bound by an antibody.

The process by which an individual's set of B-cells is able to generate all these different B-cell receptors was discovered by Susumu Tonegawa. He was given a Nobel prize in physiology or medicine for the discovery in 1987. You can read about it at nobelprize.org. 

In broad strokes, each B cell has multiple copies of different types of gene segments that make up the B cell receptor. When the B-cell develops, it selects one of each kind of segment at random and pastes them together. At the level of the human organism, this generates an awful lot of diversity, but not quite enough to produce receptors that bind every possible antigen. The additional diversity is produced by adding or deleting additional DNA bases at random.

This produces a new problem, though, because with B-cells that have receptors that would bind every possible antigen, we would end up attacking ourself. The details here are the answer to a new question, but the basic answer is that B-cells that would recognize self are selected against, leaving us with a complement of B-cells that have receptors that bind literally every possible antigen that is not self.

Lauren Sompayrac has an excellent introductory book on the immune system that makes these very difficult concepts much more approachable. It's called How the Immune System Works, and I highly recommend it.

Here's a diagram from that book, showing the random selection and pasting together involved in one of the parts of an antibody (the heavy chain):