Re: Bridge anyone? [not off topic]
Re: Bridge anyone? [not off topic]
- Subject: Re: Bridge anyone? [not off topic]
- From: Graff <email@hidden>
- Date: Wed, 03 Nov 2004 12:43:17 -0500
On Nov 3, 2004, at 11:21 AM, email@hidden wrote:
I'm pretty sure there's an even better algorithm. I know standard card
deck dealers use some kind of hash encoding of a single number to
represent a whole hand. I'm guessing that the char-math that normally
accompanies that kind of encoding won't fly easily in applescript.
There are also issues in that 32-bits (standard integer) isn't enough
to encode all possible bridge hands, so some dealers settle for a
subset of possible hands that has sufficient range, rather than
dealing with bignums.
Well here's a more traditional method for shuffling a deck: cutting and
riffling. Basically this cuts the deck at a random point, places the
bottom cut on the top and then splits the deck into two equal halves
and interleaves them. According to most of the literature the deck
will be random after 7 riffles. I get 10000 shuffles in 15 seconds for
this unoptimized script on my dual 2gHz G5. Anyone care at taking a
crack at optimizing this to see if it can get faster?
----
set sortedDeck to {"AS", "KS", "QS", "JS", "10S", "9S", "8S", "7S",
"6S", "5S", "4S", "3S", "2S", "AH", "KH", "QH", "JH", "10H", "9H",
"8H", "7H", "6H", "5H", "4H", "3H", "2H", "AD", "KD", "QD", "JD",
"10D", "9D", "8D", "7D", "6D", "5D", "4D", "3D", "2D", "AC", "KC",
"QC", "JC", "10C", "9C", "8C", "7C", "6C", "5C", "4C", "3C", "2C"}
on riffle(theDeck)
set theSize to 52
set cut to random number from 2 to 51
set d to (items (cut + 1) through theSize of theDeck) & (items 1
through cut of theDeck)
set returnDeck to {¬
item 27 of d, item 1 of d, ¬
item 28 of d, item 2 of d, ¬
item 29 of d, item 3 of d, ¬
item 30 of d, item 4 of d, ¬
item 31 of d, item 5 of d, ¬
item 32 of d, item 6 of d, ¬
item 33 of d, item 7 of d, ¬
item 34 of d, item 8 of d, ¬
item 35 of d, item 9 of d, ¬
item 36 of d, item 10 of d, ¬
item 37 of d, item 11 of d, ¬
item 38 of d, item 12 of d, ¬
item 39 of d, item 13 of d, ¬
item 40 of d, item 14 of d, ¬
item 41 of d, item 15 of d, ¬
item 42 of d, item 16 of d, ¬
item 43 of d, item 17 of d, ¬
item 44 of d, item 18 of d, ¬
item 45 of d, item 19 of d, ¬
item 46 of d, item 20 of d, ¬
item 47 of d, item 21 of d, ¬
item 48 of d, item 22 of d, ¬
item 49 of d, item 23 of d, ¬
item 50 of d, item 24 of d, ¬
item 51 of d, item 25 of d, ¬
item 52 of d, item 26 of d}
return returnDeck
end riffle
copy sortedDeck to aDeck
set theDate to current date
repeat 10000 times
repeat 7 times
set aDeck to riffle(aDeck)
end repeat
end repeat
display dialog (current date) - theDate
----
- Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden