I am attempting to create a sort of random item generator. I have a database that stores the base stats for an item. When the item is created in the world, a function randomizes the item's stats. Here is an example of my code:
toughness = randi() % (toughness + 3) + (toughness - 3)
Before this point in the code, toughness can be one of 4 values: 1, 2, 3, and 5. Now this is my understanding of how this should work, given toughness == 1 before this line of code, I would think that a random integer between -2 and 4 would be chosen. Sometimes, it chooses 0.
For the purposes of my code this is okay, toughness can be 0. However, it will throw an error saying "Division by Zero in operator '%'" and it points to this line of code. So I'm assuming this is not good code, but I'm not entirely sure why.
What exactly is randi() doing here? From my point of view, I don't see any division so I'm clearly misunderstanding how this works. Is there a better way for me to implement this? Perhaps a way that doesn't allow it to choose zero? Or is there something else I could use so that zero would be an allowable solution?
Thanks in advance for your time and consideration.