我的’randomString’函数不断返回相同的结果

我的’randomString’函数不断返回相同的结果,第1张

概述randomString.lua ------------------------------------------------------------------------------ File: randomString.lua-- Author: Don Draper-- -- This is the Lua implementation of my simple 'random 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 currentStringend

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

如果你想要一个真正随机的序列,你应该在开始生成之前用随机数随机提供真正的随机数.

总结

以上是内存溢出为你收集整理的我的’randomString’函数不断返回相同的结果全部内容,希望文章能够帮你解决我的’randomString’函数不断返回相同的结果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1219879.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存