Note that “생각이 나다” and “생각나다” very different from “생각을 하다” and “생각하다”.
생각이 나다 = 생각나다 (shorter form)
생각을 하다 = 생각하다 (shorter form and more commonly used)
As you can see, one uses a subject marker and the other an object marker.
생각이 나다 literally means “a thought came up” while 생각을 하다 means “I did a thought”.
생각이 나다 is to describe “a thought that naturally came to the mind” while 생각을 하다 is to describe the conscious/intentional effort to “think about something”.
생각이 나다 is more of a passive expression – something came to the person’s mind.
생각을 하다 is more of an active expression – the person deliberately used his/her willpower/intention/consciousness to think about something.
Which is why if you want to express about remembering something (passive in a way), you should use 생각이 나다.
On the other hand, if you want to express thinking about someone or something (definitely active), you should use 생각을 하다.
어제 시험을 볼 때 어떤 좋은 아이디어가 생각나서 시험을 잘 볼 것 같아.
During the test yesterday, some good idea came to my mind so I think I’ll do well for the test.
어제 시험을 볼 때 우리 집에서 있는 강아지를 생각해서 시험을 해낼 수 없어.
During the test yesterday, I thought about my puppy at home so I couldn’t complete the test.
Just remember that when you use 생각나다, be sure to use the subject marker 이/가 on the noun before.
As for 생각하다, always use an object marker 을/를.