1. 程式人生 > >Lua隨機問題

Lua隨機問題

nbsp 運行程序 logs style clas log code 問題 pri

1)lua隨機math.random 的結果前三個不可靠。
2)math.random(os.time())一定要寫在程序最開始的時候,不要寫在循環體內部。寫在循環體內部的math.random(os.time())無法起到每次運行程序,隨機生成隨機種子的作用。
e.g.1
math.randomseed(os.time())
for i=1,10 do
print(math.random(10))
end

結果:

2
10
5
8
6
10
6
10
3
1
[Finished in 0.1s]



e.g.2

for i=1,10 do
math.randomseed(os.time())
print
(math.random(10)) end 結果: 1 1 1 1 1 1 1 1 1 1 [Finished in 0.1s]

 

Lua隨機問題