Be careful with that question, because the popular answers make God the author of your pain. Scripture doesn't hand grief a tidy purpose. It gives you a God who is present in the suffering, holding the ending without explaining it, to be read through Jesus and not through the outcome.
The Grace Answer
When something devastating happens, sincere people rush to assign it meaning. It was part of God's plan. Everything happens for a reason. God needed another angel. He's building your character. He closed that door because He has something better. Every one of those sentences is meant to comfort, and every one of them quietly makes God the architect of your suffering, the One who could have stopped it and chose not to, so that you should now be grateful for the lesson. You are under no obligation to be grateful for the lesson.
The Bible is far more careful than that. It does not treat grief as a problem to be solved with an explanation. It gives grief a voice and lets it speak. The Psalms are full of raw complaint that never gets tied off with a moral. Psalm 88 begins in darkness and ends in darkness, and God left it in the Bible that way. When Jesus stood at the tomb of His friend, knowing He was about to raise him, He still wept. He did not explain. He entered the grief.
Present in it, not the author of it
So what does Scripture actually offer the suffering? Not a reason, but a Presence. God was present on Friday when the sky went dark, and present on Saturday, the long silent day when nothing made sense and Sunday had not come. That is where most of us live when we grieve, and He is there, holding the ending without explaining it. Pain and the "not yet" belong to this present groaning age that awaits resurrection; they are not evidence that God rations His goodness or engineered your loss.
So refuse the sentences that turn your loss into a scheduled lesson. That does not mean God is absent or uninvolved; Scripture shows Him present in suffering and able to redeem what evil, mortality, and a groaning creation have produced. It does mean you should not hand a grieving person a blueprint God never gave. Scripture does say God can form endurance, compassion, and maturity within suffering, but His ability to redeem pain does not prove He authored the event, and the fruit He may grow from it never gives us permission to invent a reason for someone else's tragedy. There is a real difference between what God can form in suffering and why a particular loss happened, and Scripture speaks far more freely about the first than the second. The safest thing you can do with your pain is refuse to interpret God through it, and interpret Him through Jesus instead, through the cross where He proved, beyond argument, that He is for you. You may never get the reason. You are not promised one. What you are promised is that you do not carry it alone.