October 17, 2004 at 9:15 pm
Does anybody have the code to simulate the random shuffling of 1 deck of cards?
October 17, 2004 at 11:06 pm
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
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
October 18, 2004 at 2:20 am
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
October 18, 2004 at 6:48 am
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.
October 18, 2004 at 10:25 am
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
October 19, 2004 at 2:29 am
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
October 19, 2004 at 8:45 am
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
October 22, 2004 at 8:07 am
Thanks to all that responded! Started building the process using NEWID().
October 26, 2004 at 6:40 am
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.
October 26, 2004 at 9:41 am
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