Hazrat Moulana Ashraf Ali Thanwi (rahmatullahi ‘alaih) once mentioned the following:
If a person is involved in sins, then one method for him to reform himself is that together with sincerely engaging in istighfaar and taubah, he should also impose some penalty upon himself so that this becomes a cause for him to refrain from falling into the sin in the future. This penalty can either be a physical penalty (e.g. fasting for a period of time) or a monetary penalty (i.e. giving a certain amount of money in sadaqah to the poor).
Rasulullah (sallallahu ‘alaihi wasallam) mentioned in the hadeeth that if a person says to someone (inviting him towards gambling), “Come, let me gamble with you,” then he should give some sadaqah.
In this hadeeth, we see that Rasulullah (sallallahu ‘alaihi wasallam) looked at the cause for one wishing to enter into the sin of gambling. The cause for this is the love for wealth. Therefore, Rasulullah (sallallahu ‘alaihi wasallam) prescribed giving some sadaqah as a penalty, as this will cause the love for wealth to leave one’s heart (and this will also prevent him from falling into the sin in the future).
(Malfoozaat Hakeemul Ummat 23/378)
Source: Ihyaauddeen.co.za