![](/img/trans.png)
[英]How can I generate random files with size 1mb each from 1-500 using linux shell script
[英]how to generate random value in each time I run the lua script
我編寫了一個名為“ lua_rand_gen”的lua腳本,其中包含以下代碼:
function random_number_func()
math.randomseed(os.time())
return (math.random(100000000,999999999))
end
print (random_number_func())
當我在終端循環中運行lua_rand_gen腳本時,上述函數未生成如下所示的randome值:
for ((i=0;i<=5;i++)); do lua lua_rand_gen; done
717952767
717952767
717952767
717952767
717952767
717952767
我知道這是因為os.time()直到一秒鍾才改變。 因此,如果運行lua腳本之間的時間差小於1秒,如何在lua中獲得隨機數。
將math.randomseed(os.time())
移動到函數外部。
這似乎是一種常見的誤解,即您每次調用math.randomseed
之前都要先調用math.random
。 這是錯誤的,並且會os.time()
math.random
的隨機性,尤其是如果您使用os.time()
作為種子,因為種子在一秒鍾的時間內都是相同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.