I made this script because I was bored and I thought I should improve my old script to generate random characters. Here’s the code, leave your thought:
local Characters = {}
for _, Charset in next, {{"A", "Z"}, {"a", "z"}, {"0", "9"}} do
for Character = string.byte(Charset[1]), string.byte(Charset[2]) do
table.insert(Characters, string.char(Character))
end
end
local function randomString(Length)
local Randomizer = Random.new(tick() * time())
local String = {}
for Character_Index = 1, (tonumber(Length) or 64) do
table.insert(String, Characters[Randomizer:NextInteger(1, #Characters)])
end
return table.concat(String)
end
One minor detail is values that are generated once and aren’t used after initialization should be put outside the function.
For example, you can just generate Characters once outside the function instead of every time inside of it. The Randomizer object can also be initialized outside once because it doesn’t rely on the state being the same.
Oh thank you for the suggestion, I’ll definitely keep that in mind. But I think it’s better to keep the Randomizer object inside of the function so it generates a new seed everytime.
You can use table.create(Length) to create a table with already allocated space for improved performance for variable String. Looks good to me otherwise
Pretty sure you get less entropy out of this, depending on how Roblox implements it. Your results don’t suddenly get worse if you keep using it. It’s weird to think about, but a new seed isn’t actually impacting the code in a good way because the seed was already random to begin with.
This should only be implemented as-needed though; regardless, while this is a good suggestion if the strings are displayed, it’s outside the scope of the supplied function.