Because it’s become somewhat of a tradition for me to do something silly on a Friday lunchtime, I thought I’d take on one of The Department‘s basic coding challenges.

The brief was simple;

I want a program that will print out the numbers 1,2,3,4,5,6,7,8,9 in a shuffled order. The order must be different each time the program runs. Note that the same number must be different each time. It should be possible to extend this to work with 52 numbers, in which case I can make a shuffled deck of cards.

You can use the Random number generator in C#, but you must make sure that the same number never appears twice, as a deck of cards which contains more than 4 aces has been known to raise suspicion.

As it was Friday lunchtime however I decided to make the solution anything but simple, firstly I replaced the numbers with the actual card names and secondly I thought I’d write it in PostScript because it demonstrates a totally different form of notation from the way we write the normal imperative languages like C# or Java.

/Suits [(Clubs)(Diamonds)(Hearts)(Spades)] def
/Cards [(Ace)(Two)(Three)(Four)(Five)(Six)(Seven)(Eight)(Nine)(Ten)(Jack)(Queen)(King)] def
/YCursorMax 720 def % 10 inches from bottom
/YCursor YCursorMax def
/XCursorMin 72 def % 1 inch from the left
/XCursor XCursorMin def
/XColWidth 113 def % 1/4 of the printable page
/Helvetica findfont
12 scalefont
setfont
/Deck [ 0 1 51 {} for ] def
0 1 50 {
/SwapLeft exch def
52 SwapLeft sub realtime rand mul exch mod
SwapLeft add /SwapRight exch def
Deck SwapLeft get
Deck SwapRight get
Deck exch SwapLeft exch put
Deck exch SwapRight exch put
} for
0 1 3 {
dup /Col exch def
0 1 12 {
Col 13 mul add Deck exch get
dup 13 mod
XCursor YCursor moveto
Cards exch get show
5 0 rmoveto
(of) show
5 0 rmoveto
13 div cvi
Suits exch get show
/YCursor YCursor 20 sub def
} for
/XCursor exch 1 add XColWidth mul XCursorMin add def
/YCursor YCursorMax def
} for
showpage

*Related*