Trying to implement 52 random numbers without repeating

I am simulating the shuffling of a card deck.  So for now lets say i want the 52 cards in random order.  Once a card is dealt it can no longer be dealt again.

This will give me 52 random numbers from 1-52.  How canI implement it so all 52 numbers will be called and none will be repeated?

    for i = 1,52 do

        j=math.random(1, 52)

        print (j)

    end 

A different strategy would seem to make sense.  Populate an array with the numbers 1-52, then randomly swap positions… (which guarantees no repeats of course).

Good stuff thank you!  I got the first part down, populating the array with numbers 1-52:

    local cards={}

    

    for i = 1,52 do

        cards[i]=i

        print (cards[i])

    end  

How do I go about randomly swapping positions?  

You want to shuffle the table.  There are numerous posts in the forums about shuffling a table.  Here is the one I use:

local function shuffleTable(t)     local rand = math.random     assert(t, "table.shuffle() expected a table, got nil")     local iterations = #t     local j          for i = iterations, 2, -1 do         j = rand(i)         t[i], t[j] = t[j], t[i]     end end

Rob

http://snippets.luacode.org/snippets/Shuffle_array_145

And if you don’t like that solution there are many more:

https://www.google.com/webhp?sourceid=chrome-instant&rlz=1C1CHFX_enUS558US558&ion=1&espv=2&ie=UTF-8#q=lua+shuffle+table

Seems the link at the top is dead.  Here is a variation on that code:

-- Shuffles the contents of a table. -- -- Returns nil. -- function table.shuffle(t) -- Algorithm altered and borrowed from http://snippets.luacode.org/snippets/Shuffle\_array\_145 local n, order = #t, {}; for i=1,n do order[i] = { rnd = math.random(), val = t[i] }; end table.sort(order, function(a,b) return a.rnd \< b.rnd; end); for i=1,n do t[i] = order[i].val; end end

@Roaminggamer,  

I am experimenting making a blackjack game and i saw a video that you have already done one.  Do you still have the source code for sale?  I don’t care if it’s pre-grapghics 2.0.  I just want to read/rewrite the code and learn whatever I can. Thanks.

Sorry.  I’m afraid that code is no longer for sale.

A different strategy would seem to make sense.  Populate an array with the numbers 1-52, then randomly swap positions… (which guarantees no repeats of course).

Good stuff thank you!  I got the first part down, populating the array with numbers 1-52:

    local cards={}

    

    for i = 1,52 do

        cards[i]=i

        print (cards[i])

    end  

How do I go about randomly swapping positions?  

You want to shuffle the table.  There are numerous posts in the forums about shuffling a table.  Here is the one I use:

local function shuffleTable(t) &nbsp;&nbsp;&nbsp; local rand = math.random &nbsp;&nbsp;&nbsp; assert(t, "table.shuffle() expected a table, got nil") &nbsp;&nbsp;&nbsp; local iterations = #t &nbsp;&nbsp;&nbsp; local j &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; for i = iterations, 2, -1 do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = rand(i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t[i], t[j] = t[j], t[i] &nbsp;&nbsp;&nbsp; end end

Rob

http://snippets.luacode.org/snippets/Shuffle_array_145

And if you don’t like that solution there are many more:

https://www.google.com/webhp?sourceid=chrome-instant&rlz=1C1CHFX_enUS558US558&ion=1&espv=2&ie=UTF-8#q=lua+shuffle+table

Seems the link at the top is dead.  Here is a variation on that code:

-- Shuffles the contents of a table. -- -- Returns nil. -- function table.shuffle(t) -- Algorithm altered and borrowed from http://snippets.luacode.org/snippets/Shuffle\_array\_145 local n, order = #t, {}; for i=1,n do order[i] = { rnd = math.random(), val = t[i] }; end table.sort(order, function(a,b) return a.rnd \< b.rnd; end); for i=1,n do t[i] = order[i].val; end end

@Roaminggamer,  

I am experimenting making a blackjack game and i saw a video that you have already done one.  Do you still have the source code for sale?  I don’t care if it’s pre-grapghics 2.0.  I just want to read/rewrite the code and learn whatever I can. Thanks.

Sorry.  I’m afraid that code is no longer for sale.