Wiktionary
pull out of the hat
vb. (context idiomatic English) To select at random, or seemingly at random.