of     1   

KiwiTronik
#183485055Saturday, February 13, 2016 5:06 AM GMT

Hi there, So i'm attempting to "shuffle" a deck of cards, which is just a table with strings of the name of each card. This way so I can deal the card in order. For some reason, I always seem to get a pattern when trying to mix the table Is there a reliable way to mix a table? My script: DeckInOrder = {"card1", "card2", "card3"} --// Obviously includes full deck function ShuffleCards() local GetCards = {} for _,v in pairs (DeckInOrder) do table.insert(GetCards, v) end ShuffledDeck = {} print'Shuffling deck... ' wait() for i = 1, #GetCards do local RandomNum = nil local RandomCard = nil repeat RandomNum = math.random(1,#GetCards) RandomCard = GetCards[RandomNum] until RandomCard ~= nil table.insert(ShuffledDeck,RandomCard) table.remove(GetCards, RandomNum) wait() end print'Finished shuffling!' return ShuffledDeck end
128Gigabytes
#183485278Saturday, February 13, 2016 5:11 AM GMT

local function shuffle(array) for x = 1, #array, 1 do local x, y = math.random(1, x), math.random(1, #array) array[x], array[y] = array[y], array[x] end return (array); end local example = {"a"; "b"; "c"; "d"; "e"; "f"; "g";} shuffle(example) for _, letter in next, (example) do print(letter) end --[[> e b c g d f a, order will very.]]
cofunction
#183485303Saturday, February 13, 2016 5:12 AM GMT

This puts all the random cards in a table and removes the old ones: local DeckInOrder = {"card1", "card2", "card3"} local RandomDeck = {} function scramble() local card = DeckInOrder[math.random(1, #DeckInOrder)] table.insert(RandomDeck, card) local num = 0 for i, v in pairs (DeckInOrder) do num = num + 1 if card == v then table.remove(DeckInOrder, num) num = 0 break end end end repeat scramble() until #DeckInOrder <=0 --View Tables for i, v in pairs (DeckInOrder) do print("Order:"..tostring(v)) end for i, v in pairs (RandomDeck) do print("Random"..tostring(v)) end
KiwiTronik
#183488950Saturday, February 13, 2016 6:38 AM GMT

Thanks guys! It seems to be working much better now :)
KiwiTronik
#183501871Saturday, February 13, 2016 2:07 PM GMT

@128g So I have 52 separate strings (cards) in the table, and the thing is I've been able to create the exact same scrambled tables... Anyway to fix that?
Veltamax
#183501921Saturday, February 13, 2016 2:08 PM GMT

put this at the very beginning of your script math.randomseed(tick())
128Gigabytes
#183502208Saturday, February 13, 2016 2:13 PM GMT

I just tested it with a while loop that wouldn't stop until it got a repeat My studio crashed because the loop didn't end.

    of     1