码迷,mamicode.com
首页 > 其他好文 > 详细

lua 中随机种子的设定

时间:2015-01-15 11:07:13      阅读:455      评论:0      收藏:0      [点我收藏+]

标签:lua   随机数   socket   随机种子   

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



lua 中随机种子的设定

标签:lua   随机数   socket   随机种子   

原文地址:http://blog.csdn.net/cjfeii/article/details/42736851

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!