我有一個2個暗淡的數組,它的所有單元都填滿了零。表值不變
我想要做的是採取一些隨機選擇的細胞,並用填充4或5
但我所得到的是空的網格與等於零的所有值或者我得到的只是一個值已更改爲4或5,這就是我下面的代碼:
local grid = {}
for i=1,10 do
grid[i] = {}
for j=1,10 do
grid[i][j] = 0
end
end
local empty={}
for i=1,10 do
for j=1,10 do
if grid[i][j]==0 then
table.insert(empty,i ..'-'.. j)
end
end
end
local fp=math.floor(table.maxn(empty)/3)
local fx,fy
for i=1,fp do
math.randomseed(os.time())
math.random(0,1)
local fo=math.random(0,1)
math.random(table.maxn(empty))
local temp= empty[math.random(table.maxn(empty))]
local dashindex=string.find(temp,'-')
fx=tonumber(string.sub(temp,1,dashindex-1))
fy=tonumber(string.sub(temp,dashindex+1,string.len(temp)))
if fo==0 then
grid[fx][fy]=4
elseif fo==1 then
grid[fx][fy]=5
end
end
for i=1,10 do
for j=1,10 do
print(grid[i][j])
end
print('\n')
end
當我調試我的代碼我得到一個正確的結果 ,但是當我運行它我不打印,每次我打印出fx和fy,並且我得到它們每次都是一樣的 – Tony
'math.randomseed(os.time())'must not在一個週期內。只是在你的代碼的開始。 –
謝謝@EgorSkriptunoff,但我也試圖乘以os.time,它的工作我每次都得到相同的值 – Tony