In U.S. federal district courts, how is the judge chosen to handle a particular case? Are judges randomly assigned to cases? Or are they chosen based on their expertise in certain areas of law?
The exact procedure varies from one district to the next, but generally speaking, it is essentially by lottery. The procedure is typically spelled out in the court's local rules. Jump to page 105 of SDNY's local rules for an example.
The lottery system is not entirely random, though. Frequently it is weighted to make it more likely that a case is assigned to a judge from the district's courthouse nearest to the parties, or to make it less likely to be assigned to the chief judge or a judge on senior status.
There are then various other rules governing assignments of cases to new judges or visiting judges, but those typically don't happen when a case is originally filed.