2021.04.24 - [이미지맥스, lua/루아 lua] - [lua] 루아에서 랜덤한 문자열을 생성하기
앞서 랜덤한 문자열을 정해진 규칙에 따라 생성해보았습니다. 가령 문자 10자 + 숫자 3자 같은 방법으로 말이죠.
이에 문자열 자동 생성을 보다 더 랜덤한 규칙으로 생성하고 싶어졌습니다.
대문자, 소문자, 숫자 캐릭터를 생성하는 부분을 각각 함수화 하고 이 함수를 랜덤으로 실행하는 방법을 통해 랜덤한 문자가 포함된 테이블을 생성합니다.
이렇게 생성한 테이블에 저장된 문자(캐릭터)들을 조합하여 문자열(스트링)으로 만들어주면 완료입니다!
< 방법1. 테이블을 사용하여 문자열 합성 (대문자, 소문자, 숫자를 생성하는 개별 함수) >
-- Make Random String
-- 대문자 생성
function makeChar1(charsTable)
local char = string.char(math.random(0x41, 0x5A))
table.insert(charsTable, char)
end
-- 소문자 생성
function makeChar2(charsTable)
local char = string.char(math.random(0x61, 0x7A))
table.insert(charsTable, char)
end
-- 숫자 생성
function makeNum(charsTable)
local char = string.char(math.random(0x30, 0x39))
table.insert(charsTable, char)
end
-- 문자열 합성
function combineString(charsTable)
local combine = ''
for i = 1, #charsTable, 1 do
combine = combine..charsTable[i]
end
return combine
end
-- sample
-- 10글자의 랜덤한 문자열을 생성하는 경우
randomChars = {}
for i = 1, 10, 1 do
make_what = math.random(1,3)
if make_what == 1 then
makeChar1(randomChars)
elseif make_what == 2 then
makeChar2(randomChars)
elseif make_what == 3 then
makeNum(randomChars)
end
end
newString = combineString(randomChars)
print(newString)
-- > HqAsc1a32q (실행할 때마다 10글자의 랜덤한 문자열이 출력)
< 방법2. 테이블을 사용하여 문자열 합성 (대문자, 소문자, 숫자를 생성하는 함수를 통합) >
-- Make Random String Functions
-- 문자 생성
function makeChar(charsTable)
make_what = math.random(1,3)
local char
if make_what == 1 then
char = string.char(math.random(0x41, 0x5A))
elseif make_what == 2 then
char = string.char(math.random(0x61, 0x7A))
elseif make_what == 3 then
char = string.char(math.random(0x30, 0x39))
end
table.insert(charsTable, char)
end
-- 문자열 합성
function combineString(charsTable)
local combine = ''
for i = 1, #charsTable, 1 do
combine = combine..charsTable[i]
end
return combine
end
-- sample
-- 10글자의 랜덤한 문자열을 생성하는 경우
randomChars = {}
for i = 1, 10, 1 do
makeChar(randomChars)
end
newString = combineString(randomChars)
print(newString)
-- > fAceG5LFs2 (실행할 때마다 10글자의 랜덤한 문자열이 출력)
< 방법3. 지정한 갯수 만큼 문자를 랜덤 생성하여 합성후 반환하는 함수 (추천) >
앞서 랜덤한 캐릭터 글자를 생성하는 함수와 이를 테이블에 저장후 스트링 문자열로 조합하는 함수를 만들었습니다.
그러면 원하는 기능 구현은 다 되었으니 이제 좀더 편하게 랜덤한 문자열을 생성해야할 차례입니다.
랜덤한 문자를 생성하는 함수와 문자를 합성하는 기능의 함수를 만들고,
생성할 글자 개수를 지정하면 랜덤한 문자열이 생성될수 있게끔 할 것입니다.
기존에 만들어두었던 생성함수와 조합함수를 활용하면 금방 만들수 있겠네요.
-- 랜덤 문자(캐릭터) 생성
function makeChar()
local new_char
local make_what = math.random(1, 3)
if make_what == 1 then
-- 대문자 A-Z 생성
new_char = string.char(math.random(0x41, 0x5A))
elseif make_what == 2 then
-- 소문자 a-z 생성
new_char = string.char(math.random(0x61, 0x7A))
elseif make_what == 3 then
-- 숫자 0-9 생성
new_char = string.char(math.random(0x30, 0x39))
end
return new_char
end
-- 랜덤 문자열(스트링) 생성
function makeString(char_amount)
local new_string = ''
if char_amount > 0 then
for i = 1, char_amount, 1 do
new_string = new_string..makeChar()
end
return new_string
else
print('argument must type more then 0')
end
end
-- >> 인수 : 생성할 글자 갯수
-- sample
-- 10글자의 랜덤한 문자열을 생성하는 경우
new_string = makeString(10)
print(new_string)
-- > Y8Mw5RYzt9 (실행할 때마다 10글자의 랜덤한 문자열이 출력)
위와 같이 함수를 정의 한뒤 실제 코드에서는 makeString() 문자열 함수 만 사용하면 되겠습니다.
이미지맥스에서 이렇게 생성한 문자열을 키보드입력함수 Keybd() 를 이용하여 타이핑하신다면 금상첨화입니다.
간편합니다.
'이미지맥스 그리고 루아(lua) > 루아(lua)' 카테고리의 다른 글
[lua] 시간 설정 및 경과 확인 함수 getClock() setClock() (0) | 2021.05.12 |
---|---|
[lua] 루아에서 sleep() 함수를 구현하기 (0) | 2021.05.10 |
[lua] 랜덤한 문자열 생성하기 -1- (기본편) (0) | 2021.04.24 |
[lua] 날짜와 시간 함수 os.date() (0) | 2021.04.17 |
[lua] 테이블 길이 확인 Table Value Lenth (0) | 2021.03.28 |