2014-02-06 44 views

回答

2
function random_90(lower, upper) 
    return math.random(math.ceil(lower/90), math.floor(upper/90))*90 
end 

print(random_90(100, 1000)) 

記得撥打math.randomseed()一次使用math.random()之前。在使用它之前最好多次調用math.random(),因爲在某些實現中,前幾個數字可能看起來不是隨機的。

math.randomseed(os.time()) 
math.random(); math.random(); math.random(); 
+0

我想你需要'math.random(math.ceil(lower/90),math.floor(upper/90))* 90',因爲Lua中沒有整數除法。 – lhf

+0

@lhf現在修復。我想過,所以我在發佈之前在我的機器上測試了代碼(Lua5.1)。它(舊代碼)起作用。我只是檢查源代碼,看起來像'math.random'獲取一個或兩個參數,在Lua 5.1中它使用'int',但在Lua 5.2中,它使用'lua_Number'。這是我不知道的一個微不足道的差異。 –

4

你可以只產生任何隨機整數和90

我從來沒有使用Lua的繁衍,但的Math.random(INT X,int y)對將產生X和Y之間的隨機整數。將其乘以90.結果將與生成的任何其他數字一樣隨機。

+0

這也適用,謝謝。 –

相關問題