4

ランダム文字列.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

ランダムな文字列の同じリストが常に生成されます。誰でも理由を説明できますか?前もって感謝します!

4

1 に答える 1