if sth such as a name is picked out of a / the hat, it is picked at random from a container into which all the names are put, so that each name has an equal chance of being picked, in a competition, etc.
out of a / the hat
if sth such as a name is picked out of a / the hat, it is picked at random from a container into which all the names are put, so that each name has an equal chance of being picked, in a competition, etc.
Definitions from the Oxford Advanced Learner's Dictionary, 9th edition