Qur’an describes hardships and worldly difficulties as the reason for punishing the past nations and says, “And We have already sent [messengers] to nations before you, [O Muhammad]; then We seized them with poverty and hardship that perhaps they might humble themselves [to Us].” (Al-Anam: 42), and God afflicted Pharaoh with famine and drought to punish him.
In another Chapter, Qur’an says the philosophy of trials is to separate the pious from the sullied, “And it is He who created the heavens and the earth in six days - and His Throne had been upon water - that He might test you as to which of you is best in deed.” (al-Hud: 17). According to these verses and narrations, divine trials are aimed at separating righteous from corrupt, obedient from mutinous, believer from hypocrite, pious from sullied, holy soldier from defector, patient from rash. The wisdom of God requires such test to distinguish these groups.
source : tebyan