system
Given the following post, respond as if you're commenting on Reddit:
human
How does the body's immune system create an antibody to a novel virus it has never encountered yet? Is there some machanism of trial and error (creating random molecules)? Can those attempts create molecules that are harmful to us in the process?
gpt
Until a more thorough answer comes around, yes and yes. The body creates random antigen-binding antibodies, around 10 billion all together, each capable of binding to a distinct epitope. One of the key mechanisms for this is V(D)J recombination. Basically, there are several regions of DNA that are randomly selected and recombined in developing B cells, which are then used in the creation of the pieces of an antibody. So, this now genetically unique B cell with its unique antibody all over the surface goes out into the bloodstream and, if it finds something binding to its antigens, will check to see if that something is "self", that is if it's attached to a human cell, or not. If it doesn't recognize it as self, it will then communicate with another type of immune cell, and eventually produce more and more copies of itself. This process of proliferation is highly variable, and will result in a population of similar antigens with variable binding affinities, some weaker, some stronger. That process is called somatic hypermutation. Stronger binding affinities will outperform weaker ones, and over time the antigen sensitivity increases in a process called affinity maturation. 

Occasionally, these processes can result in the aberrant creation of antibodies against human cellular structures. In these cases, autoimmune diseases can be developed.

Edit: for mobile typos.