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

math.random() not random?

Asked by 9 years ago

The problem is it gets stuck on the repeat meaning the until condition isn't being met. There is multiple topics in the data.Topics table.

math.randomseed(os.time()) math.random() math.random() math.random()
function m.ChooseTopic(oldtopic)
    local topic
    if oldtopic then
        repeat
            wait()
            topic = data.Topics[math.random(1, #data.Topics)]
        until not topic == oldtopic
    else
        topic = data.Topics[math.random(1, #data.Topics)]
    end
    return topic --script doesn't get this far
end

How can I make math.random be random?

1
Where is your connection line? woodengop 1134 — 9y
1
It's being fired in a different spot, I can assure you though that that is not the problem :/ YellowoTide 1992 — 9y
0
"not" can usually be used in place of ~= But the problem is the not operator has higher precedence than all operators other than ^. So you need to use parentheses to fix this. NotsoPenguin 705 — 9y

1 answer

Log in to vote
2
Answered by 9 years ago

The problem is your incorrect use of the not operator.

not topic == oldtopic

evaluates to:

(not topic) == oldtopic 
---which equals
false == oldtopic 
--which is never true

So to fix:

not (topic == oldtopic)
--Or
topic ~= oldtopic
0
All this time I thought Not and ~= were interchangeable. This has never failed me before but wow thanks! YellowoTide 1992 — 9y
Ad

Answer this question