So this comes up a lot. I often think about the case of protein structures.
Let's consider a protein of 100 amino acids; although most proteins are in the 200-500 range (IIRC) it makes the maths simpler. Even for such a short sequence, the probability of coming up with one specific sequence at random is incredibly small - 20 to the power of 100 or 1.267651e+130.
The simple answer to this apparent puzzle ('how did proteins evolve from random mutations') is of course that it did not happen like this. Not at all like monkeys typing out scripts and a human checking if they have produced a Shakespeare play yet.
In fact, there are multiple solutions to the same problem - in the analogy, this would be like multiple different versions of the same play being just as good as the other. Even better for evolution, you can - in a sense - put on a version of a play that is pretty bad, but just good enough to put in front of an audience. In other words, some enzymatic functionality can be better than none.
In this way, an evolutionary system can build up complexity by making parts that are _good enough_ and are roughly equivalent to other parts that can be replaced gradually. Combine this with the massive parallelism and the scale of the molecular world, and a billion years can accomplish a lot.