为何'randomString'函数每次都生成相同的随机字符串?

2026-04-01 19:371阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计408个文字,预计阅读时间需要2分钟。

为何'randomString'函数每次都生成相同的随机字符串?

Lua代码:简单随机字符串生成器 - 作者:Don Draper

lua-- 简单的Lua实现,用于生成随机字符串

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生成一些密码盐.但是每当我调用我的移植函数时,它总是在我第一次运行程序时返回相同的输出.

以这段代码为例.

为何'randomString'函数每次都生成相同的随机字符串?

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

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

本文共计408个文字,预计阅读时间需要2分钟。

为何'randomString'函数每次都生成相同的随机字符串?

Lua代码:简单随机字符串生成器 - 作者:Don Draper

lua-- 简单的Lua实现,用于生成随机字符串

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生成一些密码盐.但是每当我调用我的移植函数时,它总是在我第一次运行程序时返回相同的输出.

以这段代码为例.

为何'randomString'函数每次都生成相同的随机字符串?

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

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