Can somebody tell me how I can make math.random() select a random number between 20 and 80 that can only end in 0 or 5? Let me explain.
I am creating a baseball game where each player joining has custom stats, including pitch arsenals and windups. Each player also has baseball stats that can be between 20 and 80 but can only end in 5 or 0 (like in Pennant Wars, if you know what I'm talking about). Similarly to HCBB, each team has a custom stadium that would be built by cooperating players.
In conclusion, I just want to know how to manipulate math.random() as mentioned above.
Take the difference from the two values you're interpolating from, and divide it by the increment of your choice. This new quotient will be the argument you pass to random()
, multiplied by your increment, plus your minimum value. Here's an example:
local stats = {min = 20, max = 80} function stats:getIncrementBy(n) local diff = self.max - self.min return self.min + math.random(diff/n)*n end print(stats:getIncrementBy(5)) -- > random multiple of 5 between 20 and 80 print(stats:getIncrementBy(5)) -- > random multiple of 5 between 20 and 80 print(stats:getIncrementBy(5)) -- > random multiple of 5 between 20 and 80
Hope this is helped. Let me know if you have any questions!
local interval = 5 local lowerBoundary = 20 local upperBoundary = 80 function generateRandomNumber() local num = math.random(lowerBoundary/interval, upperBoundary/interval) num = interval * num return num end print(generateRandomNumber)