以下是许多可能的方法之一:
allColors = {"black", "white", "red", "orange", "yellow", "green",
"blue", "indigo", "violet", "gold", "silver", "bronze"}
chosenColors = {}
while #chosenColors < 4 do
n = math.random(1, #allColors)
table.insert(chosenColors, allColors[n])
table.remove(allColors, n)
end
然后:
for k, v in pairs(chosenColors) do
print(k, v)
end
这将打印,例如:
1 white
2 indigo
3 red
4 gold