When you have a sudden inspiration or idea for something is called a brainstorm.