Jump to content

Recommended Posts

  • Premium
for i = 1, 5 do
	local j = math.random(i, 5)
	syschat(""..i..","..j.."")
end

//return:

1,3
2,3
3,5
4,1
5,2

But i need no repeat numbers from J.

I need ALL numbers from 1 ~5 in return i   (for i = 1, 5 do) this works fine

and ALL numbers(random) from 1~5 in J

 

Like this:

1,3
2,4
3,5
4,1
5,2

// random but no repeat numbers in j

 

Any help? ?

Edited by EnKor
if pc.get_sex() == true and npc.get_sex() == false then
	npc.purge()
end

 

Link to comment
https://metin2.dev/topic/29496-return-all-numbers-from-random-function/
Share on other sites

Generate 2 sets and shuffle one of them.
 

local tabA = {} local tabB = {}
for i = 1, 5 do
	table.insert(tabA, i)
	table.insert(tabB, i)
end

-- Shuffle second tab with Yates-Fisher
for i = table.getn(tabB), 2, -1 do
	local j = math.random(i)
	tabB[j], tabB[i] = tabB[i], tabB[j]
end

print("Table A:")
for k, v in pairs(tabA) do
	print(string.format("[%d] = %d", k, v))
end
print("Table B:")
for k, v in pairs(tabB) do
	print(string.format("[%d] = %d", k, v))
end

 

  • Love 1
  • Honorable Member

The table_shuffle function from my functions file does what you want.

--[[
    Returns:
        The shuffled version of a table.

    Example:
        local table_ex = {
            [1] = "cow",
            [2] = "bird",
            [3] = "dog",
            [4] = "cat",
            [5] = "monkey"
        };

        table_shuffle(table_ex) -> returns {
            [1] = "monkey",
            [2] = "cow",
            [3] = "bird",
            [4] = "cat",
            [5] = "dog"
        };

        (Since it's random, some values may be the same as before.)
]]
table_shuffle = function(table_ex)
    local table_ex_copy = table_ex;
    local rand = 0;
    for i = table.getn(table_ex_copy), 2, -1 do
        rand = math.random(i);
        table_ex_copy[i], table_ex_copy[rand] = table_ex_copy[rand], table_ex_copy[i]
    end -- for

    return table_ex_copy;
end -- function

 

Edited by Syreldar
  • Love 1

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Don't use any images from : imgur, turkmmop, freakgamers, inforge, hizliresim... Or your content will be deleted without notice...
Use : https://metin2.download/media/add/

Please use https://metin2.download/ when uploading files smaller than 100MB, otherwise the approval will take longer due to manual upload.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.