Although dogs don't think like humans they can get frustrated and that can lead to marking in the house. Because this isn't a consistent issue, it could be medical or emotional (stress, anxiety, frustration). Consider both when looking for the solution for your dogs. When we aren't providing enough exercise and opportunities to get out and be dogs they often let us know with this sort of behaviour.