전략게시판

전략게시판 [스튜디오]
루아 스크립트 중복을 처리하는 랜덤 정수 선택 함수
2020.10.18 23:24 조회 : 2002
Lv. 332토게피 CosyClimbing 작성자 게시물 더보기
코드 :

BeforeNum = 0
DuppedNum = 0

function duppedrandom(minimum, maximum)
--    print(string.for/mat("=================="))

    local temp = Game.RandomInt(minimum, maximum)
--    print(string.for/mat("Initial RandomInt : %d",temp))
    if temp == math.abs(BeforeNum) then

--        print(string.for/mat("Dupplication detacted..."))

        DuppedNum = DuppedNum + 1

        for i=1, DuppedNum do

            temp = Game.RandomInt(minimum, maximum)

--            print(string.for/mat("New RandomInt : %d",temp))
            
            if temp ~= BeforeNum then

                BeforeNum = temp
                DuppedNum = 0

--                print(string.for/mat("New RandomInt is different!"))
--                print(string.for/mat("Final RandomInt : %d",temp))
--                print(string.for/mat("================== "))

                return temp            
            end
        end
        

--        print(string.for/mat("New RandomInt is same..."))
--        print(string.for/mat("Final RandomInt : %d",temp))
--        print(string.for/mat("================== "))

        return temp
    else

        BeforeNum = temp
        DuppedNum = 0

--        print(string.for/mat("New RandomInt is Chosen."))
--        print(string.for/mat("Final RandomInt : %d",temp))
--        print(string.for/mat("================== "))

        return temp
    end
end

설명 :

for/m 이 필터링처리를 당해서 슬래시를 집어넣음. 출력 결과를 볼 때 / 지우고 적용해야함

최소정수, 최대정수를 입력받아서 그 사이에서 랜덤한 정수값을 뽑아냄
만약 중복되는 정수가 나오면 다시 랜덤을 돌림
그래도 중복이 나온다면 신의 뜻인가보다 하고 그대로 중복결과를 내보내게 되어 있음

중복을 완전히 배제해버리면 뭔가 찝찝하길래 중복결과를 줄이는 방향으로만 짠 함수임
중복이 여러번 걸릴수록 랜덤을 다시 돌리는 횟수 역시 증가하므로
Game.RandomInt 특유의 부조리성을 해소할 수 있을 것으로 보임

(+중복을 더욱 촘촘히 걸러내고 싶으면 DuppedNum = DuppedNum + 1 에서 숫자 값을 올려주면 됨.)


사용법 :
사용하려면 duppedrandom(2, 5) 와 같이 쓰면 됨
RandomInt를 사용하기 때문에 입력값은 정수로만 쓰는게 좋음
출력 결과를 보고 싶으면 주석표시(--)를 제거할것




추천 : 1
댓글을 남기시려면 로그인 해주세요
댓글 : 0