This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Goof's profile picture

[Error] [Math.random error?]

Started by Goof, 16 October 2012 - 08:50 AM
Goof #1
Posted 16 October 2012 - 10:50 AM
Hey…
i have made this code, but it dosent work out that i suspect it to…. why?


Spoiler


function MathText()       --- this is the random words.... i want it only to show one of theese words,                         
                                            every time the function would be called...
loopRTexT = true 
while loopRTexT do
RaDM = math.random(100,130)
if RaDM == "100" then
TextDel = " C:\System32x\data135 "
end
if RaDM == "101" then
TextDel = " C:\System32x\data134 "
end
if RaDM == "102" then
TextDel = " C:\System32x\data133 "
end
if RaDM == "103" then
TextDel = " C:\System32x\data132 "
end
if RaDM == "104" then
TextDel = " C:\System32x\data131 "
end
if RaDM == "105" then
TextDel = " C:\System32x\data130 "
end
if RaDM == "106" then
TextDel = " C:\System32x\data129 "
end
if RaDM == "107" then
TextDel = " C:\System32x\data128 "
end
if RaDM == "108" then
TextDel = " C:\System32x\data127 "
end
if RaDM == "109" then
TextDel = " C:\System32x\data126 "
end
if RaDM == "110" then
TextDel = " C:\System32x\data125 "
end
if RaDM == "111" then
TextDel = " C:\System32x\data124 "
end
if RaDM == "112" then
TextDel = " C:\System32x\data123 "
end
if RaDM == "113" then
TextDel = " C:\System32x\data122 "
end
if RaDM == "114" then
TextDel = " C:\System32x\data121 "
end
if RaDM == "115" then
TextDel = " C:\System32x\data213 "
end
if RaDM == "116" then
TextDel = " C:\System32x\data123 "
end
if RaDM == "117" then
TextDel = " C:\System32x\data213 "
end
if RaDM == "118" then
TextDel = " C:\System32x\data2213 "
end
if RaDM == "119" then
TextDel = " C:\System32x\data29 "
end
if RaDM == "120" then
TextDel = " C:\System32x\data30 "
end
if RaDM == "121" then
TextDel = " C:\System32x\data31 "
end
if RaDM == "122" then
TextDel = " C:\System32x\data32 "
end
if RaDM == "123" then
TextDel = " C:\System32x\data33 "
end
if RaDM == "124" then
TextDel = " C:\System32x\data44 "
end
if RaDM == "125" then
TextDel = " C:\System32x\data2 "
end
if RaDM == "126" then
TextDel = " C:\System32x\data211 "
end
if RaDM == "127" then
TextDel = " C:\System32x\data24 "
end
if RaDM == "128" then
TextDel = " C:\System32x\data21 "
end
if RaDM == "129" then
TextDel = " C:\System32x\data1 "
end
if RaDM == "130" then
TextDel = " C:\System32x\data11 "
end



loopRTexT = false
end
end


ma = math.random(2,13) -- random times of showing/reshowing the "random words"
for i = 1,ma do
MathText() -- Calls the "get Random words" Function..
term.clear()
term.setCursorPos(1,5)
print("+ CHECKING ->  "..TextDel.." +")  -- the print... this is line 113, and it says the error at the end   
                                                                    of this post...
sleep(1)
end



i want a print, that prints random words (from another function) and then print it… but it says:


startup:113: attempt to concentate nil and string
Kingdaro #2
Posted 16 October 2012 - 11:11 AM
Your checking RaDM against strings, not numbers.

...
if RaDM == 100 then
TextDel = " C:System32xdata135 "
end
if RaDM == 101 then
TextDel = " C:System32xdata134 "
end
if RaDM == 102 then
TextDel = " C:System32xdata133 "
end
...
Goof #3
Posted 16 October 2012 - 11:29 AM
but how can i change that?
remiX #4
Posted 16 October 2012 - 12:07 PM
He showed you. In each line, remove the quotation marks "'s from the numbers.


if RaDM == "100" then


to


if RaDM == 100 then


etc..
robhol #5
Posted 16 October 2012 - 12:25 PM
Better yet, use a table instead of a long list of redundant "if"s.
Kingdaro #6
Posted 16 October 2012 - 01:12 PM
Better yet, use a table instead of a long list of redundant "if"s.
I agree, but sadly, our job isn't to make scripts more efficient unless asked to do so. :S
Lyqyd #7
Posted 16 October 2012 - 03:51 PM
Better yet, use a table instead of a long list of redundant "if"s.
I agree, but sadly, our job isn't to make scripts more efficient unless asked to do so. :S

Sure it is. In cases like this where there's an obvious better solution than the way it's been done (such as indexing a table rather than an enormous if tree), we should be pointing that out. We are here to help people learn and improve, not just as a more verbose syntax error detection system.
Goof #8
Posted 16 October 2012 - 09:18 PM
TY for all the Answers… The program works fine, now… thank you very much….
robhol #9
Posted 17 October 2012 - 11:36 AM
I agree, but sadly, our job isn't to make scripts more efficient unless asked to do so. :S
If I see a way to improve a script (and can be bothered), I will.