Card Deck Simulation

  • Does anybody have the code to simulate the random shuffling of 1 deck of cards?

  • In what format do you want the results presented? Random order of numbers 1-52 in a table?

    Regards

    Phil

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • An easy way to generate a 'random' sequence is to order by NEWID()

    Here's showing the principle:

    create table #deck ( card int not null )

    declare @i int

    set @i = 1

    while @i < 53

     begin

      insert #deck select @i

      set @i = @i + 1

     end

    -- deal a hand

    select top 5 card from #deck order by newid()

    -- shuffle whole deck

    select card from #deck order by newid()

    /Kenneth

  • Alternatives to Kenneth's

    select number

    from master.dbo.spt_values

    where type ='P'

    and number between 1 and 52

    order by newid()

    select top 5 number

    from master.dbo.spt_values

    where type ='P'

    and number between 1 and 52

    order by newid()

    Far away is close at hand in the images of elsewhere.
    Anon.

  • I think this will work. I wanted to simulate shuffling and dealing hands.

    I can assign a card to each number like this:

    #1 = 2 of Hearts, #2 = 3 of Hearts and so forth....

     

    Thanks

  • The point with my example (and David's) was just to show how to shuffle using NEWID()

    Just create your 'deck' table with the cards you need, and then you could probably just order by NEWID() to get a reasonably random hand.

    /Kenneth

  • Joe,

    I'm pretty sure that none of the Perfect Shuffle algorithms had a DBMS in mind! Do you really believe that a cursor is better than the ORDER BY NEWID() solution? The latter will perform better, take a lot less code to write, and will produce acceptable results. And both solutions use proprietary syntax. So what's the pro-cursor argument?

    --
    Adam Machanic
    whoisactive

  • Thanks to all that responded! Started building the process using NEWID().

  • The only question I have is if you need to keep track of the cards that have been dealt. You wouldn't want a dealer playing a Second Ace of Spades in a later hand if the shuffle should not occurr between each play.

    If that is the case then you may want to store the run in another table during play to keep the shuffle in the current order and so you can mark out of play as they pass or even better place in a cursor (yes on purpose) and proceed thru it until not enough cards remain for new play. Then reshuffle at that point.

    Otherwise I support using the NEWID() becuase there is no random function in SQL that will meet the needs. Technically NEWID() thou is not random but it will change the sequence enough to be far superior to RAND() or going out of your way to write a custom piece of code.

  • Is this a college course question?

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply