Re: That is not the algorithm I proposed

From: Brian Hetrick (bhetrick_at_notinnedmeats.iname.com)
Date: 11/23/04


Date: Tue, 23 Nov 2004 00:49:28 -0500


"Paul Tomkins" <tomkinsp@iinet.net.au> wrote ...

[snip]
> I actually proposed deckB[i] = deckC[deckA[i]].
[snip]

Indexing through a deckX array is essentially applying a permutation.
If deckA and deckC are both shuffled (randomized), appears to be no
benefit in the double indexing -- unless you are hypothesizing some
very peculiar properties of the underlying random number generator.

I also have a very hard time understanding your pseudocode, so I have
taken the liberty of generating real code for the simple permutation
algorithm. The permutation algorithm is in the ShuffleDeck procedure.

----- Code -----
{$G128,P1024}

PROGRAM Permute (Input, Output);

CONST
    NumberCards = 52;
    NumberRanks = 13;
    NumberSuits = 4;

TYPE
    Suit = (Club, Diamond, Heart, Spade);
    Rank = (Deuce, Trey, Four, Five, Six, Seven, Eight, Nine, Ten,
            Jack, Queen, King, Ace);
    Card = RECORD
        Suit : Suit;
        Rank : Rank
        END;
    Deck = ARRAY [1 .. NumberCards] OF Card;
    SuitString = String [8];
    RankString = String [7];
    CardString = String [19];

VAR
    MyDeck : Deck;
    CardIndex : Integer;

FUNCTION DescribeSuit
   ( Card : Card) : SuitString;

    TYPE
        SuitTable = RECORD
            Suit : Suit;
            Name : SuitString
        END;

    CONST
        Suits : ARRAY [1 .. NumberSuits] OF SuitTable =
           ((Suit: Club; Name: 'Clubs'),
            (Suit: Diamond; Name: 'Diamonds'),
            (Suit: Heart; Name: 'Hearts'),
            (Suit: Spade; Name: 'Spades'));

    VAR
        Index : Integer;

    BEGIN

    DescribeSuit := 'Unknown';
    FOR Index := 1 TO NumberSuits
    DO
        BEGIN

        IF Card.Suit = Suits [Index].Suit
        THEN
            BEGIN

            DescribeSuit := Suits [Index].Name

            END
        END
    END;

FUNCTION DescribeRank
   ( Card : Card) : RankString;

    TYPE
        RankTable = RECORD
            Rank : Rank;
            Name : RankString
        END;

    CONST
        Ranks : ARRAY [1 .. NumberRanks] OF RankTable =
           ((Rank: Deuce; Name: 'Deuce'),
            (Rank: Trey; Name: 'Trey'),
            (Rank: Four; Name: 'Four'),
            (Rank: Five; Name: 'Five'),
            (Rank: Six; Name: 'Six'),
            (Rank: Seven; Name: 'Seven'),
            (Rank: Eight; Name: 'Eight'),
            (Rank: Nine; Name: 'Nine'),
            (Rank: Ten; Name: 'Ten'),
            (Rank: Jack; Name: 'Jack'),
            (Rank: Queen; Name: 'Queen'),
            (Rank: King; Name: 'King'),
            (Rank: Ace; Name: 'Ace'));

    VAR
        Index : Integer;

    BEGIN

    DescribeRank := 'Unknown';
    FOR Index := 1 TO NumberRanks
    DO
        BEGIN

        IF Card.Rank = Ranks [Index].Rank
        THEN
            BEGIN

            DescribeRank := Ranks [Index].Name

            END
        END
    END;

FUNCTION DescribeCard
   ( Card : Card) : CardString;

    VAR
        Description : CardString;

    BEGIN

    Description := DescribeRank (Card);
    Insert (' of ', Description, Length (Description) + 1);
    Insert (DescribeSuit (Card), Description, Length (Description) +
1);
    DescribeCard := Description

    END;

PROCEDURE InitializeCard
   (VAR Card : Card;
        Index : Integer);

    BEGIN

    Index := Index - 1;
    Card.Suit := Suit (Index DIV NumberRanks);
    Card.Rank := Rank (Index MOD NumberRanks)

    END;

PROCEDURE InitializeDeck
   (VAR Deck : Deck);

    VAR
        CardIndex : Integer;

    BEGIN

    FOR CardIndex := 1 TO NumberCards
    DO

        InitializeCard (Deck [CardIndex], CardIndex)

    END;

PROCEDURE ShuffleDeck
   (VAR Deck : Deck);

    VAR
        FirstIndex : Integer;
        SecondIndex : Integer;
        SwapTemp : Card;

    BEGIN

    FOR FirstIndex := 1 TO NumberCards - 1
    DO
        BEGIN

        SecondIndex := FirstIndex + Random (NumberCards - FirstIndex +
1);
        SwapTemp := Deck [FirstIndex];
        Deck [FirstIndex] := Deck [SecondIndex];
        Deck [SecondIndex] := SwapTemp

        END
    END;

PROCEDURE PrintDeck
   ( Deck : Deck);

    VAR
        CardIndex : Integer;
        PrintPosition : Integer;
        Description : CardString;
        EndingIndex : Integer;

    BEGIN

    PrintPosition := 0;
    FOR CardIndex := 1 TO NumberCards
    DO
        BEGIN

        Description := DescribeCard (Deck [CardIndex]);
        EndingIndex := PrintPosition;
        IF PrintPosition > 0
        THEN
            BEGIN

            EndingIndex := EndingIndex + 2

            END;
        EndingIndex := EndingIndex + Length (Description);
        IF CardIndex <> NumberCards
        THEN
            BEGIN

            EndingIndex := EndingIndex + 1

            END;
        IF EndingIndex > 70
        THEN
            BEGIN

            WriteLn (',');
            PrintPosition := 0

            END
        ELSE
            BEGIN

            IF PrintPosition > 0
            THEN
                BEGIN

                Write (', ');
                PrintPosition := PrintPosition + 2

                END
            END;
        Write (Description);
        PrintPosition := PrintPosition + Length (Description)

        END;
    WriteLn
    END;

BEGIN

InitializeDeck (MyDeck);
WriteLn ('Original deck:');
PrintDeck (MyDeck);
ShuffleDeck (MyDeck);
WriteLn ('After shuffling:');
PrintDeck (MyDeck)

END.
----- End of Code -----
----- Sample Output -----
Original deck:
Deuce of Clubs, Trey of Clubs, Four of Clubs, Five of Clubs,
Six of Clubs, Seven of Clubs, Eight of Clubs, Nine of Clubs,
Ten of Clubs, Jack of Clubs, Queen of Clubs, King of Clubs,
Ace of Clubs, Deuce of Diamonds, Trey of Diamonds, Four of Diamonds,
Five of Diamonds, Six of Diamonds, Seven of Diamonds,
Eight of Diamonds, Nine of Diamonds, Ten of Diamonds,
Jack of Diamonds, Queen of Diamonds, King of Diamonds,
Ace of Diamonds, Deuce of Hearts, Trey of Hearts, Four of Hearts,
Five of Hearts, Six of Hearts, Seven of Hearts, Eight of Hearts,
Nine of Hearts, Ten of Hearts, Jack of Hearts, Queen of Hearts,
King of Hearts, Ace of Hearts, Deuce of Spades, Trey of Spades,
Four of Spades, Five of Spades, Six of Spades, Seven of Spades,
Eight of Spades, Nine of Spades, Ten of Spades, Jack of Spades,
Queen of Spades, King of Spades, Ace of Spades
After shuffling:
Six of Diamonds, Ace of Spades, Seven of Diamonds, Six of Clubs,
Trey of Hearts, Queen of Spades, King of Diamonds, Nine of Spades,
Trey of Spades, Five of Spades, Jack of Hearts, Ten of Clubs,
King of Spades, Ten of Spades, Seven of Clubs, Jack of Clubs,
Nine of Clubs, Jack of Diamonds, Deuce of Hearts, Queen of Clubs,
Seven of Hearts, Queen of Hearts, Ten of Diamonds, Trey of Diamonds,
Four of Diamonds, Five of Clubs, Eight of Hearts, Ace of Hearts,
Trey of Clubs, Seven of Spades, Nine of Diamonds, Ace of Diamonds,
Nine of Hearts, Eight of Spades, Deuce of Diamonds, Six of Hearts,
Four of Clubs, Five of Hearts, Queen of Diamonds, Five of Diamonds,
Four of Hearts, King of Clubs, Eight of Clubs, Six of Spades,
Deuce of Spades, Jack of Spades, King of Hearts, Ten of Hearts,
Four of Spades, Deuce of Clubs, Ace of Clubs, Eight of Diamonds
----- End of Sample Output -----



Relevant Pages

  • herding global vars to a module
    ... a='Three of clubs' ... a='Three of diamonds' ... a='Three of hearts' ... a='Three of spades' ...
    (comp.lang.fortran)
  • Re: herding global vars to a module
    ... foo(50, 11,Queen of spades,'s'),& ... b='Three of clubs' ... b='Three of diamonds' ... b='Three of hearts' ...
    (comp.lang.fortran)
  • Re: how to bid this slam
    ... Clubs A K Q 9 8 5 ... Spades K ... Hearts A J 10 9 8 ... Diamonds K 3 2 ...
    (rec.games.bridge)
  • Re: how to bid this slam
    ... Clubs A K Q 9 8 5 ... Spades K ... Hearts A J 10 9 8 ... Diamonds K 3 2 ...
    (rec.games.bridge)
  • Re: For you percentage mavens
    ... RHO played small and I won the Queen. ... those spades are my entries. ... If I played Ace of clubs, AK of spades, spade ruff, King of clubs, club, RHO ... gets in with the Ace of hearts, they have a diamond to cash. ...
    (rec.games.bridge)