我的’randomString’函数不断返回相同的结果
发布时间:2020-12-14 21:45:40 所属栏目:大数据 来源:网络整理
导读:randomString.lua ------------------------------------------------------------------------------ File: randomString.lua-- Author: Don Draper-- -- This is the Lua implementation of my simple 'randomString' function-- which I previous wrote i
randomString.lua
---------------------------------------------------------------------------- -- File: randomString.lua -- Author: Don Draper -- -- This is the Lua implementation of my simple 'randomString' function -- which I previous wrote in PAWN. ---------------------------------------------------------------------------- randomString = {} local randomCharset = { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9" } function randomString.generate(length) local currentString = "" for index = 1,length do currentString = currentString .. randomCharset[math.random(1,#randomCharset)] end return currentString end Test.lua require("randomString") print(randomString.generate(16)) io.read() 所以这是我最初用PAWN编程语言写的’randomString’函数,我想我会把它实现给Lua生成一些密码盐.但是每当我调用我的移植函数时,它总是在我第一次运行程序时返回相同的输出. 以这段代码为例. for i = 0,100 do print(randomString.generate(16)) end 它将始终生成相同的随机字符串列表.任何人都可以解释原因吗?提前致谢! 解决方法
math.random生成一系列伪随机数,这是一个类似于实际随机序列的确定性序列.除非使用
math.randomseed ,否则math.random生成的序列将始终相同.
对于您调用math.randomseed的每个可能值,math.random将生成不同的伪随机序列. 例如,试试这个,你会看到一个不同的序列: math.randomseed( 7654321 ) for i = 0,100 do print(randomString.generate(16)) end 如果你想要一个真正随机的序列,你应该在开始生成之前用随机数随机提供真正的随机数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |