I'm not a developer, and pretty much participate as a hobbyist.
For my game, I want a random mechanic, dropping weapons with random stats and rarity.
Here's my process :

Every second, a number is generate : Chance, Attack and Defense.
My Chance variable correspond to the % of dropping rare (and more powerful) weapons.
- If Chance < 60 : COMMON (Attack and Defense will be between 1 and 60)
- If Chance >= 60 and < 90 : RARE (Attack and Defense will be between 61 and 79)
- If Chance >= 90 : LEGENDARY (Attack and Defense will be between 80 and 100)
So yo get :
- 60% change getting Common Weapon
- 30% chance getting Rare Weapon
- 10% chance getting Legendary Weapon
When you click the button, Attack and Defense are retrieved and fixed from their variable. So I can display the item with correct number, that will add up to the global Attack/Defense.
It took me not so long to figure it out, so it's cool.
You guessed it. I'm making a LootBoxes game. Cause... the more you have, the worse it is.
EDIT : I was overcomplicating stuff here... I got rid of generator for simple click to random instead.