2016-02-21 48 views
-1

我做了這個程序:有沒有辦法在for循環中爲lua中的變量添加數值?

a = math.random(0, 9) - 0 -- In this stage, if the result equals zero, that means it's a match 
b = math.random(0, 9) - 1 
c = math.random(0, 9) - 2 
d = math.random(0, 9) - 3 
e = math.random(0, 9) - 4 
f = math.random(0, 9) - 5 
g = math.random(0, 9) - 6 
h = math.random(0, 9) - 7 
i = math.random(0, 9) - 8 
j = math.random(0, 9) - 9 

print("Enter the number of trials you want to simulate.") -- This is where I decide how many trials I want to do 

var = io.read() 

a_ = 0 -- This is where I hope to keep the number of "a" matches, number of "b" matches, etc. The frequency 
b_ = 0 
c_ = 0 
d_ = 0 
e_ = 0 
f_ = 0 
g_ = 0 
h_ = 0 
i_ = 0 
j_ = 0 

for k = 1, var do -- One loop is a trial 

    print("Trail #"..k) 

    if a == 0 then 
     print("a = match") 
    elseif a ~= 0 then 
     print("a = not a match") 
    end 

    if b == 0 then 
     print("b = match") 
    elseif b ~= 0 then 
     print("b = not a match") 
    end 

    if c == 0 then 
     print("c = match") 
    elseif c ~= 0 then 
     print("c = not a match") 
    end 

    if d == 0 then 
     print("d = match") 
    elseif d ~= 0 then 
     print("d = not a match") 
    end 

    if e == 0 then 
     print("e = match") 
    elseif e ~= 0 then 
     print("e = not a match") 
    end 

    if f == 0 then 
     print("f = match") 
    elseif f ~= 0 then 
     print("f = not a match") 
    end 

    if g == 0 then 
     print("g = match") 
    elseif g ~= 0 then 
     print("g = not a match") 
    end 

    if h == 0 then 
     print("h = match") 
    elseif h ~= 0 then 
     print("h = not a match") 
    end 

    if i == 0 then 
     print("i = match") 
    elseif i ~= 0 then 
     print("i = not a match") 
    end 

    if j == 0 then 
     print("j = match") 
    elseif j ~= 0 then 
     print("j = not a match") 
    end 

end 

while true do --This is just to keep the window open after the program is done so that I can observe the data, you can ignore this 
end 

正如你所看到的,我想添加一個A_,B_ C_和每次返回結果爲零的時間,但它不工作,它有辦法去做這個? 我想這樣做的原因是我正在接受一個AP的統計類,這會讓事情變得更容易。我現在只是在做a_,b_,c_,一旦我解決了這個問題,我會全部做完。謝謝閱讀!

+1

請提供您的代碼的簡化版本,您的代碼很長,而且沒有組織。 – warspyking

+0

我不知道如何簡化它,對不起。我對Lua不是很擅長,這是我能做的最好的。有關簡化的任何提示? – ZackaryCW

+0

如果這有助於我的目標是 – ZackaryCW

回答

0

假設你想模擬運行「變種」次,試試這個:

math.randomseed(os.time()) 

local matchStorage = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
local randomNums = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 

local function runSimulation(numTrial) 
    print("\nRunning trial #: " .. numTrial) 

    for index, value in pairs(randomNums) do 
     local variable = math.random(0, 9) 
     randomNums[index] = variable 
    end 

    for index, value in pairs(randomNums) do 
     if randomNums[index] == 0 then 
      matchStorage[index] = matchStorage[index] + 1 
      print("index " .. index .. " is a match.") 
     else 
      print("index " .. index .. " is not a match.") 
     end 
    end 
end 

do 
    print("Enter the number of trials") 
    numTrials = io.read() 

    for index = 1, numTrials do 
     runSimulation(index) 
    end 

    print("\nRESULTS:\n") 

    for index, value in pairs(matchStorage) do 
     print("index " .. index .. " frequency: " .. value) 
    end 
end 

倍以下的「randomNum」值之一包含0將被存儲在其相應的「matchStorage數'索引。

+0

OMG非常感謝您的幫助,我能夠做10萬次試驗。這幫助了SOOO很多。我無法感謝你爲我整個程序重新寫作。不是我正在尋找的那種頻率,但我會再次修復thx – ZackaryCW

相關問題