Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make math.random() go by random intervals of 5?

Asked by 6 years ago

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.

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
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)
0
I can wait to test this later, I don't currently have my laptop on me. DeceptiveCaster 3761 — 6y

Answer this question