52 Deck Card Shuffling/Randomizer (no repeats)

How would I go about making a standard card shuffling mechanism? Such as, handing out a certain amount of cards with no repeats.

52 cards in a deck with 4 different suits and 13 different valued cards.

Are you looking for something like Fisher-Yates shuffle?

function shuffle(tbl)
	for i = #tbl, 2, -1 do
		local j = math.random(i)
		tbl[i], tbl[j] = tbl[j], tbl[i]
	end
	return tbl
end

If you donā€™t want them to repeat I guess you could create a table and take the cards away so they wonā€™t repeat? I am not sure because I donā€™t play cards but here is what I found on google.

3 Likes

You donā€™t actually need to shuffle the cards, you can just have a random number generator pick 5 numbers from 1 to 52 where each number corresponds to a different card. Then when they are picked, remove them from the table and go again.

1 Like

Basically, like what @DataSigh explained, all of the cards are unique.

How would I determine which card is chosen though, without having 52 if statements?

Such as number 3 being pulled by the randomizer, how would the script know that that is the Ace of hearts?

Perhaps storing them as reference? Likeā€¦

local cards = {
	[3] = "Ace of hearts"
}

print(cards[numberPicked]) -- prints "Ace of hearts" when numberPicked is 3
2 Likes

How could I change the amount of cards picked from that?

Oh wait, @Daw588 would that be numberPicked?

Thatā€™s the card number to obtain information about the card.

You are picking just one. You could store the amount of cards for each player if thats what you want.

Is there a way I could use that to pick multiple cards at once?

Same card or different card? If you could provide an example It would help me understand your problem.

Different cards, such as:

I want 5 different cards to be chosen from the deck of 52.

This was my initial idea too, but writing out 52 lines of code is kinda ugly

local cardTypes = {
  suits = {
    "Hearts",
    "Clubs",
    "Diamonds",
    "Spades",
  },

  type = {
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10",
    "Jack",
    "Queen",
    "King",
  },
}

local suitType = cardTypes["suits"][math.random(1, 4)]
local cardType = cardTypes["type"][math.random(1, 13)]
print(cardType .. " of " .. suitType)

image

You can do this as the first step to categorize the card types. You should try to attempt the next part on your own

1 Like

You can put all of the card references/information in a module and require it.

cards.lua

return {} -- card information

main.lua

local cards = require(script.cards)
-- cards[index]
math.randomseed(tick())
local partchoice = math.random(1,40) -- will become 52
	local numberPicked = partchoice

local cards = {
	[1] = "Ace of Spades",
	[2] = "Ace of Clubs",
	[3] = "Ace of Hearts",
	[4] = "Ace of Dimonds",
	[5] = "2 of Spades",
	[6] = "2 of Clubs",
	[7] = "2 of Hearts",
	[8] = "2 of Dimonds",
	[9] = "3 of Spades",
	[10] = "3 of Clubs",
	[11] = "3 of Hearts",
	[12] = "3 of Dimonds",
	[13] = "4 of Spades",
	[14] = "4 of Clubs",
	[15] = "4 of Hearts",
	[16] = "4 of Dimonds",
	[17] = "5 of Spades",
	[18] = "5 of Clubs",
	[19] = "5 of Hearts",
	[20] = "5 of Dimonds",
	[21] = "6 of Spades",
	[22] = "6 of Clubs",
	[23] = "6 of Hearts",
	[24] = "6 of Dimonds",
	[25] = "7 of Spades",
	[26] = "7 of Clubs",
	[27] = "7 of Hearts",
	[28] = "7 of Dimonds",
	[29] = "8 of Spades",
	[30] = "8 of Clubs",
	[31] = "8 of Hearts",
	[32] = "8 of Dimonds",
	[33] = "9 of Spades",
	[34] = "9 of Clubs",
	[35] = "9 of Hearts",
	[36] = "9 of Dimonds",
	[37] = "10 of Spades",
	[38] = "10 of Clubs",
	[39] = "10 of Hearts",
	[40] = "10 of Dimonds",
	-- will be added on to until 52
}

print(cards[numberPicked])

This is what I started working on lol
How could I make my randomizer shown above print multiple values?

I made some code to generate that for you ahahaha

local cardTypes = {
  suits = {
    "Hearts",
    "Clubs",
    "Diamonds",
    "Spades",
  },

  type = {
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10",
    "Jack",
    "Queen",
    "King",
  },
}

local generalCardTable = table.create(52)

for _, suitT in pairs(cardTypes["suits"]) do
  for _, typeT in pairs(cardTypes["type"]) do
    table.insert(generalCardTable, (typeT .. " of " .. suitT))
  end  
end

print(generalCardTable)

1 Like

Yeah thats a good idea, having Enums in their own module folder is a smart way to organize a project.

ohhh, Iā€™m sorry, I didnā€™t scroll down on your first example of the code. I thought you just showed another way of organizing the values. Thank you!

It should be pretty straightforward from here. math.random(1,52 - i) ā† this 5 times then you can either make the index equal to nil or use table.remove

1 Like
local cards = {}
local suits = {"Clubs", "Diamonds", "Hearts", "Spades"}
local faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}

for _, suit in pairs(suits) do
	for _, face in pairs(faces) do
		local card = face.." of "..suit
		table.insert(cards, card)
	end
end

local function randCard()
	return math.random(1, 52)
end

for i = 1, 5 do
	local randNum = randCard()
	local card = cards[randNum]
	print(card)
end

Output:

image

image

1 Like