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

Is it possible to have multiple ranges in a math.random function?

Asked by 2 years ago

I searched around a bit, but couldn't find much on the topic. I am wondering if instead of math.random(1,20) I could have two ranges like math.random([5,10], [13,20]), where x (the random number) can only be between 5 and 10 or between 13 and 20.

Any advice is appreciated.

0
math.random(math.random(10,20),math.random(10,20))? Puppynniko 1059 — 2y

3 answers

Log in to vote
2
Answered by 2 years ago
Edited 2 years ago

This is probably what you want, as good as it gets as well

local RNGranges = function(...) -- {num,num},{num,num}
    local r = math.random(1,#({...}))
    return math.random(({...})[r][1],({...})[r][2])
end

print(RNGranges({5,10},{15,20},{5,6},{11,12},{9990,99999}))

Puppynniko's answer will give an error anyways after constant testing.

Basically the code is saying: "Give me a bunch of tables with ranges that you want to math.random() and I will provide a random table with the rng value."

How to use? The comment I wrote states {num,num},{num,num} where in the code, the first random range is 5,10 and then the second is 15,20.

Easy to use.

The best part about this is that you can have infinite ranges, you can put stuff like:

RNGranges({5,20},{60,100},{4,6},{99492349,43293249329})

as well as simple stuff like RNGranges({10,100})

To prove that this doesn't pick anything outside of the ranges, check this out: tester code

0
my comment is not a answer  Puppynniko 1059 — 2y
0
My bad, I meant to say comment* it was worth a shot though greatneil80 2647 — 2y
0
wonderful answer although i dont understand half of it Puppynniko 1059 — 2y
0
Yeah your answer probably works but I just did trial and error until I found my own solution a couple hours after this post. Stiles8353 35 — 2y
0
Your answer uses recursion which takes up more memory and runs more code, this code I stated would eliminate that as well as clean up your code greatneil80 2647 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

I looked for a while and I'm pretty sure there's no good way of doing it so this is what I came up with.

local one = math.random(5,20)

if one > 10 and one < 13 then

    local choice = math.random(1,2)

    if choice == 1 then
        one = math.random(5,10)
    else
        one = math.random(13,20)
    end

end

Basically it will work just fine if its in range but if its not then it will go through the number 1 or 2 and then do one of those ranges then keep that number, if you want to repeat the number multiple times then just add a while true do loop.

Log in to vote
0
Answered by 2 years ago

Found it out myself after a while;

                repeat 
                    num = math.random(-30, 30)
                until num > 10 or num < -10

                repeat 
                    num2 = math.random(-30, 30)
                until num2 > 10 or num2 < -10

Answer this question