At the beginning of Romans 5, the Apostle Paul is speaking of the glories of justification by faith.
Therefore, since we have been justified by faith, we have peace with God through our Lord Jesus Christ. Through him we have also obtained access by faith into this grace in which we stand, and we rejoice in hope of the glory of God.
But beginning in verse 3, Paul seems to randomly change topics:
Not only that, but we rejoice in our sufferings, knowing that suffering produces endurance, and endurance produces character, and character produces hope.