2016-02-21 51 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

相关问题