• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua randomize calculations in lua

Mjmackan

Mapper ~ Writer
Premium User
Joined
Jul 18, 2009
Messages
1,424
Solutions
15
Reaction score
176
Location
Sweden
Yello!

I want to randomize between negative or positive aka - and +.

I've tried to simply:
Lua:
local randSign = math.random(1,2)
    if randSign == 1 then
        randMe = "+"
    else
        randMe = "-"
    end

doSendMagicEffect({x = (pos.x ..randMe.. 3), y = (pos.y ..randMe.. 3), z = pos.z, stackpos = 255}, 50)

But as shown its a string and can therefor not be used, I know you can convert strings to numbers and vice versa, is it possible to convert string to calculation variables aswell?
I know there is workarounds to solve the problem "to work as intended", but It would be fun to know if there actually are something useable in lua to convert these things.
 
math.random works fine with negative numbers, why can't you use that?
pic is math.random(-10,10)

Edit: didn't see streamside
 

Attachments

Yello!

I want to randomize between negative or positive aka - and +.

I've tried to simply:
Lua:
local randSign = math.random(1,2)
    if randSign == 1 then
        randMe = "+"
    else
        randMe = "-"
    end

doSendMagicEffect({x = (pos.x ..randMe.. 3), y = (pos.y ..randMe.. 3), z = pos.z, stackpos = 255}, 50)

But as shown its a string and can therefor not be used, I know you can convert strings to numbers and vice versa, is it possible to convert string to calculation variables aswell?
I know there is workarounds to solve the problem "to work as intended", but It would be fun to know if there actually are something useable in lua to convert these things.

Lua:
    local pos = player:getPosition()
    local randSign = math.random(1,2)
    local randMe = ''
    if (randSign == 1) then
        randMe = '+'
    else
        randMe = '-'
    end
    local x, y, z = (pos.x ..randMe.. 3), (pos.y ..randMe.. 3), pos.z
    doSendMagicEffect({x = pos.x, y = pos.y, z = pos.z, stackpos = 255}, 50)
 
math.random(-10, 10)

math.random works fine with negative numbers, why can't you use that?
pic is math.random(-10,10)

Edit: didn't see streamside

Lua:
local offset = 3
local value = math.random(2) == 1 and offset or -offset
doSendMagicEffect({x = pos.x + value, y = pos.y + value, z = pos.z, stackpos = 255}, 50)
- + = (+)
+ + = (+)
And leaving it empty will declare it to absolute nothing when a positive number comes. (x = pos.x 3)

Lua:
    local pos = player:getPosition()
    local randSign = math.random(1,2)
    local randMe = ''
    if (randSign == 1) then
        randMe = '+'
    else
        randMe = '-'
    end
    local x, y, z = (pos.x ..randMe.. 3), (pos.y ..randMe.. 3), pos.z
    doSendMagicEffect({x = pos.x, y = pos.y, z = pos.z, stackpos = 255}, 50)

Not sure of what your thought about this were.
 
you can also make randMe = 1 and randMe = -1 and calculate value * randMe to give it positive or negative value

or even do this:
value = math.random(1,2) == 1 and value or -value
in this case "= ... and" functions a bit like "if ... then" and "or" functions a bit like "else" statement
 
- + = (+)
+ + = (+)
And leaving it empty will declare it to absolute nothing when a positive number comes. (x = pos.x 3)

1000 + -3 = 997
1000 + 3 = 1003

I'm not sure what you mean when you say both outcomes bring a positive number?
Post automatically merged:

To answer your original question, yes you can, but it's terrible to use.
(In Lua 5.1, use loadstring instead of load.)

Make sure you trust the source though; this can be used to run arbitrary code.
So don't use this method in a talkaction, for example.

Lua:
local pos = {x = 1000, y = 1000, z = 7}
local x = ""
local y = ""
local randMe
local func

local randSign = math.random(1,2)
if randSign == 1 then
    randMe = "+"
else
    randMe = "-"
end

x = pos.x .. randMe .. 3
func = assert(load("return " .. x))
x = func()

y = pos.y .. randMe .. 3
func = assert(load("return " .. y))
y = func()

print(x)
print(y)
 
Last edited:
@Xikini you're overcomplicating that.

tonumber(string.format("%s%d", "-", 10)) would be enough
Ah, nice. lmao
Makes it a nice one-liner.

Lua:
x = pos.x + tonumber(string.format("%s%d", randMe, 3))
y = pos.y + tonumber(string.format("%s%d", randMe, 3))

print(x)
print(y)
 
Back
Top