-- Prime the pseudo-random number generator with a unique enough seed.
math.randomseed(os.time());
-- Take an array-like table and shuffle it.
local function shuffleArray(array)
local shuffledArray = {};
for _, element in ipairs(array) do
local position = math.random(1, #shuffledArray + 1);
table.insert(shuffledArray, position, element);
end
return shuffledArray;
end
-- Convert a string to an array-like table where every element is a single
-- character.
local function stringToCharacterArray(str)
local characters = {};
for character in str:gmatch(".") do
table.insert(characters, character);
end
return characters;
end
-- Scramble an integer, converting it to a number where every character in
-- the original number is in a random position.
local function scrambleInteger(integer)
local numberAsString = tostring(integer);
local characters = stringToCharacterArray(numberAsString);
local shuffledCharacters = shuffleArray(characters);
local shuffledNumberAsString = table.concat(shuffledCharacters, "");
local shuffledNumber = tonumber(shuffledNumberAsString);
return shuffledNumber;
end
-- Run a few tests
print(scrambleInteger(1234))
print(scrambleInteger(1234))
print(scrambleInteger(1234))
print(scrambleInteger(1234))
print(scrambleInteger(1234))
When just running this script in an empty baseplate, the tests worked! Here was the output:
This seems pretty complicated for such a simple thing. While your solution works fine, I’m sure, here’s a quicker and (in my opinion) cleaner version contained in one function for anyone curious:
local function shuffleNumber(int)
local digits = {}
for letter in tostring(int):gmatch(".") do
table.insert(digits, math.random(#digits + 1), letter)
end
return tonumber(table.concat(digits, ""))
end
Basically, you can just randomly pick where to insert rather than inserting each letter and then shuffling the list.
It certainly is a less verbose function! I like it a lot.
I tend to make snippets like this though, where I’m unsure of the developer’s familiarity with certain semantics, to make code as verbose as possible such that even if they don’t understand the fine-grained details (like what gmatch does, et cetera), they can understand the gist.
I’m also used to languages like Ruby, Swift, or ECMAScript where the standard library typically has this functionality in it as opposed to the lightweight library we have with Lua, so I broke it up in a more-than-needed modular way.