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

How do i make a random value lower than a specific number value?

Asked by
CjayPlyz 643 Moderation Voter
6 years ago
Edited 6 years ago

If you don't like reading skip to the end

So i have 3 random number

local random1 = math.random(1,10)
local random2 = math.random(1,10)
local random3 = math.random() -- or math.random(0,1)

Now let's say if the value of the random3 is 0 then the value of random1 and random2 can be anywhere between 1 and 10, but if the value of random3 is 1 then the value of random2 must be lower than random1. But the value of random 1, 2 must always be between 1 and 10.

local random3  = math.random()
if random3 == 0 then
    local random1 = math.random(1,10)
    local random2 = math.random(1,10)
else -- or else if random3 == 1 then
    local random1 = math.random(2,10)
    local random2 =
end

Now the question is in line 7, how do i make the value lower than the value i get on random1, for example if i get the value of 5 on random1 then the value of random2 must be lower than 5 which means it must be between 1 and 4, i also made the random1 get a value between 2,10 because if it gets 1 then that won't work because there isn't anything lower than 1 that is higher than 1.

I mean i could do this

local random3  = math.random()
if random3 == 0 then
    local random1 = math.random(1,10)
    local random2 = math.random(1,10)
else -- or else if random3 == 1 then
    local random1 = math.random(2,10)
    repeat
        local random2 = math.random(1,9)
    until random2 < random1
end

it would work but if there is an alternative way other than using repeat or a long list of "if else" then i would take it. And also i need to change 2 textlabel from the 2 random values so, the current script would be like this.

local number1 = workspace.Number1.SurfaceGui.TextLabel
local number2 = workspace.Number2.SurfaceGui.TextLabel

local function variable ()
    local random3  = math.random()
    if random3 == 0 then
        local random1 = math.random(1,10)
        local random2 = math.random(1,10)
        number1.Text = tostring(random1)
        number2.Text = tostring(random2)
    else -- or else if random3 == 1 then
        local random1 = math.random(2,10)
        number1.Text = tostring(random1)
        repeat
            local random2 = math.random(1,9)
            number2.Text = tostring(random2)
        until random2 < random1
    end
end

anyeventlistener:Connect(variable)

So overall the problem is.. How do i make a random value lower than another random value

0
Probably the longest question i made so far in scripting helpers CjayPlyz 643 — 6y
0
Do you need it to be a whole number, or can there be decimals? 1TheNoobestNoob 717 — 6y
0
^ not part of the question and totally irrelevant. User#21908 42 — 6y
0
Not likely, but that repeat until of yours could, in theory, go on forever. User#21908 42 — 6y
View all comments (4 more)
0
Not irrelevant. 1TheNoobestNoob 717 — 6y
0
Commented an answer after realizing I left my browser with this page open for 20 mins. Nvm. xPolarium 1388 — 6y
0
`math.random() -- or math.random(0,1)` It returns a number between 0 and 1, not just 0 and 1. https://www.robloxdev.com/articles/Lua-Libraries/math#random TheeDeathCaster 2368 — 6y
0
^ i only said what i found CjayPlyz 643 — 6y

1 answer

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

Just use the variable random1 as your second argument to math.random. For example:

local random3  = math.random()
if random3 == 0 then
    local random1 = math.random(1,10)
    local random2 = math.random(1,10)
else -- or else if random3 == 1 then
    local random1 = math.random(2,10)
    local random2 = math.random(whatevernumberhere, random1)
end

I hope this helps and have a great day scripting! Edit: The same is applicable if you want the second random number to be greater than the first:

local random3  = math.random()
if random3 == 0 then
    local random1 = math.random(1,10)
    local random2 = math.random(1,10)
else -- or else if random3 == 1 then
    local random1 = math.random(2,10)
    local random2 = math.random(random1, whatevernumberhere)
end
Ad

Answer this question