According to the Bible, when I receive a blessing in my life, God is behind it (James 1:17). Also according to the Bible, when I walk through horrendous trials in my life, God is behind it (James 1:2-4). In fact, according to the Bible, just about the only thing that we can say that God is not behind is the temptation to sin that we experience. James 1:13-15 makes it very clear that such temptations come from the sin that dwells within us. We dare not cast the blame for our sinful desires on God! It is our own indwelling sin that rises up and produces fruit - God does not implant some alien sinful desire in us - it is wholly natural. What is fascinating about all of this, though, is that God is very much in control of the consequences of the fruit of our sinful desires, and our sin leads us into various trials. For what trial or hardship would ever have arisen outside of the sinful state of humanity? Would we suffer hunger or sickness or loss of loved ones? No, all hardship is a result of our transgressions of God's perfect law. But apparently, not all hardship is an actual wrathful, just punishment for our transgressions of God's perfect law.
In fact, in James 1:2, we are even told to "count is all joy...when you meet trials of various kinds." We are told that such trials produce steadfastness, which - when allowed to exercise its full effect - makes us "perfect and complete, lacking in nothing" (verse 4).
So, trials are good, but trials comes as a result of sin, and sin is born through the ripening of temptation. The start of this chain reaction takes place in our own wicked hearts, and the end is a pure product of God - something that verse 17 of James 1 calls a "good and perfect gift from above, coming down from the Father of Lights." But if that's the case, then why does it feel like He's punishing us?
I think this is where we have to make a distinction between loving discipline and just punishment. A just punishment for our sins would be an immediately enforced, eternally enduring Second Death. This was the wrath that Christ took for us on the cross when He suffered in our place (if we are truly believers). A loving discipline, however, is a measure designed by a loving Father to keep a child away from a just punishment.
When I discipline my daughter for taking away something from her sister, it is not because I hate her. It's not even really because I'm angry with her. I discipline her because I want her to learn how to live righteously so that she will avoid a just retribution (from God and from the state) for a crime like stealing.
So, in a situation like that, my daughter actually ought to thank me for her stinging backside. It was a good gift coming from her father. It was meant to produce steadfastness, which, if allowed to have its full effect, would lead her toward perfection and completeness. It was given completely in love and ought to be received with total thankfulness.
I sin, but God still just keeps handing out the good and perfect gifts. Sometimes they seem to come in response to something I've done, and sometimes they just seem to come out of the blue. But all of the time they are loving gifts, designed by a perfect Father to accomplish a perfect purpose in my life, whether they be heapings of joy-filled blessings or long valleys of soul-crushing trials. One is just as good as another in God's perfect plan, and so I have to learn to "count it all joy."