1. 常用的方法:
math.randomseed(os.time()) print(math.random())
2. 改进的方法:
math.randomseed(tostring(os.time()):reverse():sub(1, 6)) print(math.random())
3. 再改进的方法:
local socket = require("socket") -- 需要用到luasocket库 local function get_seed() local t = string.format("%f", socket.gettime()) local st = string.sub(t, string.find(t, "%.") + 1, -1) return tonumber(string.reverse(st)) end math.randomseed(get_seed()) print(math.random())
ref: http://blog.csdn.net/zhangxaochen/article/details/8095007
时间: 2024-11-06 01:53:10