How many times it has happened that you are ok, and then suddenly at next moment you are angry and without any reason, there is nothing that has happened in your life, or anything that you can think that makes sense.
Why does this happens? Subconscious, you have thought about something just for a micro/mili second and it has been processed by your subconscious mind and it is not showing its results which are totally not in your hand. what can you do about this issue? Think about only good things? as I said before its not about you thinking about wrong thing, in-fact its not at all in your hand, you can not do anything about it.
Your subconscious keeps throwing thoughts at you and keep processing those thoughts, So basically your life is 80% run by your subconscious and merely 20% by what you are doing consciously. Most of the things that you are doing consciously are driven by your subconscious.
What is the solution? Read something good when you go-to bed and when you wakeup. So, the first thing of the day and the last thing of the day has to be something good, not some gossip, not thinking about what bad your in-laws did to you, what your neighbors are doing to you and how they are lying about small small things. Thinking nothing about that is going to help you.
I guess the only solution is to train your brain that even subconsciously it should not throw any bad thought at you making you sad or angry.