I hadn't known that there would be such a problem since I hadn't used RPG Maker or the Ruby language for a long time.
It would have been nice if I could provide the perfect logic as the first comment, but at the time, it was not an environment where RPG Maker could be run, so I couldn't verify it, so this problem arose.
In the code in the previous comment, the part that used Math.rand() * n was modified to use Random.rand(n). I checked whether the logic works normally in RPG Maker VX Ace, so there should be no problem now.
I hope this has been helpful in creating your work.