473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Shuffle card deck

JC
I am very new to programming and learning on my own.

Why do I keep getting duplicate values using this code? I want to shuffle a
deck of 52 cards. The logic seems right to me.

Randomize

For C = 0 To 1000

C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51

C2 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
Cards(C1) = C2 ' Value of C2 is assigned to Card in location Cards(C1)
Cards(C2) = C1 ' Value of C1 is assigned to Card in location Cards(C2)

Next C

TIA
Jul 17 '05 #1
23 12892
"JC" <no************@hotmail.com> wrote in
news:bk**********@sun-news.laserlink.net:
I am very new to programming and learning on my own.

Why do I keep getting duplicate values using this code? I want to
shuffle a deck of 52 cards. The logic seems right to me.

Randomize

For C = 0 To 1000

C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51

C2 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
Cards(C1) = C2 ' Value of C2 is assigned to Card in location
Cards(C1) Cards(C2) = C1 ' Value of C1 is assigned to Card in
location Cards(C2)

Next C


Your random values are not guaranteed to be unique. When shuffling
cards you have only one value of each card in a normal deck.

You need an entirely different algorithm.

Following is an example of defining a deck of cards, including the
shuffle routine, using the Ada language:

First I define a package specification for cards:

-----------------------------------------------------------------------
-- Cards.ads
-- Package implementing a standard deck of playing cards
-----------------------------------------------------------------------

package Cards is

type Card is private;

-- Print the value of a card
procedure Print(Item : in Card);

type Deck is private;

-- Create an initial deck (open a new deck of cards)
function Fill_Deck return Deck;

-- Print all the cards remaining in a deck
procedure Print(Item : in Deck);

-- Shuffle the deck (randomize the order of the cards in the deck)
procedure Shuffle(The_Deck : in out Deck);

-- Deal the next card from the deck
procedure Deal(The_Card : out Card; From : in out Deck);

-- Return the number of cards left in the deck
function Cards_Left(In_Deck : Deck) return Natural;

-- Deck_Empty exception raised when trying to deal from an empty deck.
Deck_Empty : Exception;

private

-- Define the face values of the cards
type Pips is (Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
Jack, Queen, King, Ace);

-- Define the card suits
type Suits is (Hearts, Spades, Clubs, Diamonds);

-- A card is defined by its combination of face value
-- and suit.
type Card is record
Pip : Pips;
Suit : Suits;
end record;

-- Define the number of cards in a standard deck.
subtype Deck_Index is integer range 1..52;

-- Cards in the deck are accessed through an order list.
-- The values in the order list are sorted to create a
-- shuffled deck.
type Order_List is array(Deck_Index) of Deck_Index;

-- A deck is an order list, an index into the order list
-- indicating the next card to deal, and a count of the
-- number of cards left (not yeat dealt) in the deck.
type Deck is record
This_Order : Order_List;
Deal_Next : Deck_Index := Deck_Index'First;
Num_Left : Natural := 0;
end record;
end Cards;

Next I define the implementation of all the routines declared in
the package specification above:

-----------------------------------------------------------------------
-- Cards.adb
-- Implementation of the Cards package
-----------------------------------------------------------------------

with Ada.Numerics.Float_Random;
with Ada.Text_Io;

package body Cards is

----------------
-- The Card_Deck array is the only array in this package
-- actually containing Card objects.
-- A Deck is a data structure with an array of indexes into
-- this single Card_Deck.
-- A Deck is shuffled by randomizing that array of indexes.
----------------
type Card_Deck is array(Deck_Index) of Card;

--------------
-- Internal Function: Initialize
-- Purpose: Initialize the value of the common Card_Deck
-- This function is called only once during the elaboration of
-- this package. It is used to initialize the values in the
-- All_Decks array.
--------------
function Initialize return Card_Deck is
Result : Card_Deck;
Temp_Index : Integer := Deck_Index'First;
begin
for The_Suit in Suits loop
for The_Pip in Pips loop
Result(Temp_Index) := (The_Pip, The_Suit);
Temp_Index := Temp_Index + 1;
end loop;
end loop;
return Result;
end Initialize;

All_Decks : constant Card_Deck := Initialize;

-----------
-- Procedure: Print
-- Purpose: Print the value of a card on standard output
-- The enumeration labels will be printed using instantiations
-- of the generic package Ada.Text_IO.Enumeration_Io.
-----------
procedure Print(Item : in Card) is
package Pip_Io is new Ada.Text_Io.Enumeration_IO(Pips);
package Suit_Io is new Ada.Text_Io.Enumeration_Io(Suits);
begin
Pip_Io.Put(Item => Item.Pip);
Ada.Text_Io.Put(Item => " of ");
Suit_Io.Put(Item => Item.Suit);
Ada.Text_Io.New_Line;
end Print;

-----------------
-- Function: Fill_Deck
-- Purpose: Create a new card deck with all cards in order
----------------
function Fill_Deck return Deck is
Result : Deck;
begin
for Temp_Index in Deck_Index'Range loop
Result.This_Order(Temp_Index) := Temp_Index;
end loop;
Result.Num_Left := Deck_Index'Last;
return Result;
end Fill_Deck;

---------
-- Procedure: Print
-- Purpose: Print all the cards remaining in the deck
---------

procedure Print(Item : in Deck) is
begin
if Item.Num_Left > 0 then
for Temp_Index in Item.Deal_Next..Deck_Index'Last loop
Print(All_Decks(Item.This_Order(Temp_Index)));
end loop;
else
Ada.Text_Io.Put_Line("The deck is empty.");
end if;
end Print;

------------
-- Procedure Swap
-- Exchange two Deck_Index values
-- This procedure is visible only to functions and procedures
-- defined in this package body.
--------------
procedure Swap(Left, Right : in out Deck_Index) is
Temp : Deck_Index := Left;
begin
Left := Right;
Right := Temp;
end Swap;

-------------
-- Procedure: Shuffle
-- Purpose: Randomize the This_Order array for a deck to force
-- random access to the deck of cards
--
-- This algorithm is order O(n) and will work with any discrete
-- index type.
-- The Ada.Numerics.Float_Random routine is used so that the
-- random number generator is reset only once per shuffle. This
-- produces more random results than can be achieved by
-- resetting the generator for each iteration as would be needed
-- if the Ada.Numerics.Discrete_Random package had been used.
------------

procedure Shuffle(The_Deck : in out Deck) is
use Ada.Numerics.Float_Random;
Seed : Generator;
-- Max_Search is set to the 2nd to last value in Deck_Index
Max_Search : Deck_Index := Deck_Index'Pred(Deck_Index'Last);
Difference : Integer;
Rand_Value : Integer;
Swap_Val : Deck_Index;
begin
Reset(Seed);
The_Deck.Deal_Next := Deck_Index'First;
The_Deck.Num_Left := Deck_Index'Last;
for Index in Deck_Index'First .. Max_Search loop
Difference := Deck_Index'Pos(Deck_Index'Last) -
Deck_Index'Pos(Index);
Rand_Value := Integer( Random(Seed) * Float(Difference)) +
Deck_Index'Pos(Index);
Swap_Val := Deck_Index'Val(Rand_Value);
Swap(The_Deck.This_Order(Index),
The_Deck.This_Order(Swap_Val));

end loop;
end Shuffle;

--------------------------------------------------------------------
-- The Deal procedure produces the next card from the deck.
-- It also updates the Deck data structure to account for
-- another card being removed from the Deck, and finally, it
-- updates the Deck's Deal_Next index to point to the next card
-- to deal from the deck.
-------------------------------
procedure Deal(The_Card : out Card; From : in out Deck) is
begin
if From.Num_Left > 0 then
The_Card := All_Decks(From.This_Order(From.Deal_Next));
From.Num_Left := From.Num_Left - 1;
if From.Deal_Next < Deck_Index'Last then
From.Deal_Next := From.Deal_Next + 1;
end if;
else
-- raise the Deck_Empty exception
raise Deck_Empty;
end if;
end Deal;

--------------------------------------------------------------------
-- The function Cards_Left simply reports the number of cards not
-- yet dealt from the deck. This number can be anything from 0
-- through Deck_Index'Last.
--------------------------------------------------------------------
function Cards_Left(In_Deck : Deck) return Natural is
begin
return In_Deck.Num_Left;
end Cards_Left;
end Cards;

Finally, I show a procedure to create a deck of cards and do the
shuffling:

-----------------------------------------------------------------------
-- Card_Deck.adb
-- This procedure is a test driver for the Cards package
-----------------------------------------------------------------------

with Ada.Text_Io;
with Cards;

procedure Card_Deck is

My_Deck : Cards.Deck;
This_Card : Cards.Card;

begin

-- Create a new deck of cards, like opening a new deck of
-- cards. The deck returned is sorted by suit and value.
My_Deck := Cards.Fill_Deck;
Ada.Text_Io.Put_Line("Initial Deck:");
Cards.Print(My_Deck);

-- Shuffle the deck so that the cards are accessed in a
-- random order.
Cards.Shuffle(My_Deck);
Ada.Text_Io.New_Line(2);
Ada.Text_Io.Put_Line("Shuffled Deck:");
Cards.Print(My_Deck);

-- Deal out the cards, printing each dealt card.
Ada.Text_Io.New_Line(2);
Ada.Text_Io.Put_Line("Printing each card as it is dealt:");
while Cards.Cards_Left(In_Deck => My_Deck) > 0 loop
Cards.Deal(The_Card => This_Card, From => My_Deck);
Cards.Print(This_Card);
end loop;

-- Attempt to deal one more card from the deck. This will raise
-- the Deck_Empty exception.
Ada.Text_Io.New_Line(2);
Ada.Text_Io.Put_Line("Attempting to deal from an empty deck:");

-- the following unnamed block encapsulates its own exception
-- handler for the Cards.Deck_Empty exception.
begin
Cards.Deal(The_Card => This_Card, From => My_Deck);
Cards.Print(This_Card);
exception
when Cards.Deck_Empty =>
Ada.Text_Io.Put_Line(
"ERROR: You attempted to deal from an empty deck.");
end;

-- Attempt to print an empty deck
Cards.Print(My_Deck);

end Card_Deck;

Jim Rogers
Jul 17 '05 #2
JC

"James Rogers" <ji**************@att.net> wrote in message
news:Xn******************************@204.127.36.1 ...
"JC" <no************@hotmail.com> wrote in
news:bk**********@sun-news.laserlink.net:
I am very new to programming and learning on my own.

Why do I keep getting duplicate values using this code? I want to
shuffle a deck of 52 cards. The logic seems right to me.

Randomize

For C = 0 To 1000

C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51

C2 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
Cards(C1) = C2 ' Value of C2 is assigned to Card in location
Cards(C1) Cards(C2) = C1 ' Value of C1 is assigned to Card in
location Cards(C2)

Next C


Your random values are not guaranteed to be unique. When shuffling
cards you have only one value of each card in a normal deck.

You need an entirely different algorithm.

Following is an example of defining a deck of cards, including the
shuffle routine, using the Ada language:
Jim Rogers


Thanks for the response. What I am looking for is just a Randomizing
routine that could be applied to any situation.

I have already created an array for the cards. I just want to randomize
the index which I address them by, but sometimes 2 or even three cards of
same suit and value are being drawn. I am guessing that my index itself has
to have the duplicate values, since each of my 52 card array is unique.
Don't know how much sense this makes to anyone.

I wrote a video poker program on my Commodore 64 about twenty years ago,
which was the first and last time I ever tried to write a real program. It
worked great in that I got it to do exactly what I wanted, and I used a
routine similar to the above, without have duplicate cards showing up.

Jul 17 '05 #3
In your code, C1 and C2 are not the indexes of the cards, they are the values.
Yet you use them to decide which two cards to exchange, instead of the indexes
where they came from.

Say the first Int(Rnd * 52) came up 22, and the second one came up 37. Say that
Cards(22) = 17, and Cards(37) = 24. Your code would then change the value of
Cards(17) to 24 (same as Cards(37), which is unchanged), and change Cards(24) to
17 (same as Cards(22), also unchanged). Now you have two 17's and two 22's.

Try substituting this in the loop:

c1 = (Int(Rnd * 52)) ' returns an index from 0 to 51
c2 = (Int(Rnd * 52)) ' returns an index from 0 to 51

temp = Cards(c1) 'save value of cards(c1)
Cards(c1) = Cards(c2) 'assign cards(c2) to cards(c1)
Cards(c2) = temp 'assign old cards(c1) to cards(c2)

If c1 = c2, there is not much point in swapping a card with itself, so you could
put in a test to skip those.

Steve

"JC" <no************@hotmail.com> wrote in message
news:bk**********@sun-news.laserlink.net...
I am very new to programming and learning on my own.

Why do I keep getting duplicate values using this code? I want to shuffle a
deck of 52 cards. The logic seems right to me.

Randomize

For C = 0 To 1000

C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51

C2 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
Cards(C1) = C2 ' Value of C2 is assigned to Card in location Cards(C1)
Cards(C2) = C1 ' Value of C1 is assigned to Card in location Cards(C2)

Next C

TIA

Jul 17 '05 #4
JC

"Steve Gerrard" <no*************@comcast.net> wrote in message
news:YT********************@comcast.com...
In your code, C1 and C2 are not the indexes of the cards, they are the values. Yet you use them to decide which two cards to exchange, instead of the indexes where they came from.

Say the first Int(Rnd * 52) came up 22, and the second one came up 37. Say that Cards(22) = 17, and Cards(37) = 24. Your code would then change the value of Cards(17) to 24 (same as Cards(37), which is unchanged), and change Cards(24) to 17 (same as Cards(22), also unchanged). Now you have two 17's and two 22's.
Try substituting this in the loop:

c1 = (Int(Rnd * 52)) ' returns an index from 0 to 51
c2 = (Int(Rnd * 52)) ' returns an index from 0 to 51

temp = Cards(c1) 'save value of cards(c1)
Cards(c1) = Cards(c2) 'assign cards(c2) to cards(c1)
Cards(c2) = temp 'assign old cards(c1) to cards(c2)

If c1 = c2, there is not much point in swapping a card with itself, so you could put in a test to skip those.

Steve


That's it! I know I shouldn't try to think on an empty brain...... I was not
assigning Cards(c1) = Cards(c2) but instead, going straight to Cards(c1) =
c2.

Thanks, Steve!
Jul 17 '05 #5
"JC" <no************@hotmail.com> wrote in
news:bk**********@sun-news.laserlink.net:
Thanks for the response. What I am looking for is just a Randomizing
routine that could be applied to any situation.
The Shuffle procedure in my example does what you want.
It is a reasonably efficient way to randomize a collection of objects
and avoid duplicates.

I have already created an array for the cards. I just want to
randomize
the index which I address them by, but sometimes 2 or even three cards
of same suit and value are being drawn. I am guessing that my index
itself has to have the duplicate values, since each of my 52 card
array is unique. Don't know how much sense this makes to anyone.
There is nothing wrong with moving a single card more than once
during a shuffle. What is wrong is having the same card value more
than once in a single deck.

Your deck is not properly constructed if you are generating the values
more than once.

Note that the example I showed created and initialized a deck of cards
before shuffling them. No new card values were created. Only the order
of the values was changed.

My example optimized this a bit by using an ordering array, rather than
actually moving about the cards in the card deck. The reason for this
is that the cards may be constructed using a larger data representation,
including information such as suit and pip value. Moving large values
is less efficient than simply moving index values. My example simply
moves around the index values.

Using this scheme, I only have one deck of actual card values. The user
"deck" really contains an ordering array of index values. This allows
minimum use of memory and maximum shuffling speed. Users can appear to
have individual decks, but all decks refer to a single set of actual
card values. Each user simply sees those values in a different order.

I wrote a video poker program on my Commodore 64 about twenty years
ago,
which was the first and last time I ever tried to write a real
program. It worked great in that I got it to do exactly what I wanted,
and I used a routine similar to the above, without have duplicate
cards showing up.


My suspicion is that your current routine is somehow different from the
one you used twenty years ago. That difference just might be important.

Jim Rogers
Jul 17 '05 #6
xyz
' initialize the deck
For i = 1 To 52
cards(i) = i
Next i

' shuffle the deck
' this shuffling assures that each card
' changes position at least once
For N = 52 To 2 Step -1
K = Int(N * Rnd) + 1
TEMP = cards(N)
cards(N) = cards(K)
cards(K) = TEMP
Next N

' array cards(i) now has the shuffled deck
==========
On Tue, 23 Sep 2003 20:39:21 -0700, "JC" <no************@hotmail.com>
wrote:
I am very new to programming and learning on my own.

Why do I keep getting duplicate values using this code? I want to shuffle a
deck of 52 cards. The logic seems right to me.

Randomize

For C = 0 To 1000

C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51

C2 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
Cards(C1) = C2 ' Value of C2 is assigned to Card in location Cards(C1)
Cards(C2) = C1 ' Value of C1 is assigned to Card in location Cards(C2)

Next C

TIA


Jul 17 '05 #7
JC wrote:
I am very new to programming and learning on my own.

Why do I keep getting duplicate values using this code? I want to shuffle a
deck of 52 cards. The logic seems right to me.

Randomize

For C = 0 To 1000

C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51

C2 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
Cards(C1) = C2 ' Value of C2 is assigned to Card in location Cards(C1)
Cards(C2) = C1 ' Value of C1 is assigned to Card in location Cards(C2)


When you swap two things you need a temporary**! Why loop to 1000? You
can loop just 52 times if you swap each card with a randomly chosen other.

for(int c=0; c<52; ++c)
{
Card &a = cards[c];
Card &b = cards[rnd(52)]; // For a suitably defined rnd

Card t = a; // Not expensive if Card is 4 bytes or 1 byte
a = b;
b = t;
}
** There is also an XOR hack:
a = a^b, b = a^b, a = a^b;
saves a register spill :-)

Jul 17 '05 #8
In article <Xn******************************@204.127.36.1>,
James Rogers <ji**************@att.net> wrote:
.... -- Define the number of cards in a standard deck.
subtype Deck_Index is integer range 1..52;
Just curious -- why not

subtype Deck_Index is integer range Pips'Range * Suits'Range;

Is that not possible? That would be consistent with the Initialize
function:
function Initialize return Card_Deck is
Result : Card_Deck;
Temp_Index : Integer := Deck_Index'First;
begin
for The_Suit in Suits loop
for The_Pip in Pips loop
Result(Temp_Index) := (The_Pip, The_Suit);
Temp_Index := Temp_Index + 1;
end loop;
end loop;
return Result;
end Initialize;


Cheers,
Gorazd
--
Gorazd Bozic <gb*@email.si>
Jul 17 '05 #9
> I am very new to programming and learning on my own.

Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me.

Randomize

For C = 0 To 1000

C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51

C2 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
Cards(C1) = C2 ' Value of C2 is assigned to Card in location Cards(C1)
Cards(C2) = C1 ' Value of C1 is assigned to Card in location Cards(C2)

Next C


You already have answers to your main question, but I thought I'd offer this
previous post of mine in case the 2nd method used in it seemed useful to
you.

Rick - MVP

Not sure what "Hit or Miss" solitaire is, but here are two versions of a
card shuffling routine (depending on how you store the cards) that I have
posted previously.

FIRST METHOD
=================
The following is a generalized "shuffling" routine. Give it an array of
elements and it will put them in random order and return the randomized
elements back in the original array that was passed to it. It only visits
*each* array element *once* so it is quick. The code takes care of running
the Randomize statement one time only (which is all that is necessary).

Sub RandomizeArray(ArrayIn As Variant)
Dim X As Long
Dim RandomIndex As Long
Dim TempElement As Variant
Static RanBefore As Boolean
If Not RanBefore Then
RanBefore = True
Randomize
End If
If VarType(ArrayIn) >= vbArray Then
For X = UBound(ArrayIn) To LBound(ArrayIn) Step -1
RandomIndex = Int((X - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))
TempElement = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(X)
ArrayIn(X) = TempElement
Next
Else
'The passed argument was not an array
'Put error handler here, such as . . .
Beep
End If
End Sub

The passed array may be of any normal type -- integer, string, single, etc.
The neat thing is, if you pass an already randomized array to this routine,
those randomly ordered elements will be randomize -- sort of like shuffling
an already shuffled deck of cards. In your case, simply set up the array
(probably Global or Static if you want to use it over and over) something
like this

Dim DeckOfCards(1 To 52) As Long
For X = 1 To 52
DeckOfCards(X) = X
Next

and to shuffle (randomize) it, simply call

RandomizeArray DeckOfCards

Each array element will now hold a unique, random number from 1 through 52
for the above example.
SECOND METHOD
=================
Here is another take on the same routine which actually returns "named"
cards such as 3 of Hearts (here your DeckOfCards is declared as a String:

Sub ShuffleDeck(Deck() As String)
Dim X As Integer
Dim TempInt As Integer
Dim TempCard As String
Static TempDeck(1 To 52) As String
Static RanBefore As Boolean
If Not RanBefore Then
RanBefore = True
Randomize
If UBound(Deck) <> 52 Then
'Programmer passed an improper array
MsgBox "Deck array is dimensioned incorrectly"
Exit Sub
ElseIf TempDeck(52) = "" Then
'Initialize the deck of cards
For X = 1 To 52
If ((X - 1) Mod 13) = 0 Then
TempDeck(X) = "Ace"
ElseIf ((X - 1) Mod 13) = 10 Then
TempDeck(X) = "Jack"
ElseIf ((X - 1) Mod 13) = 11 Then
TempDeck(X) = "Queen"
ElseIf ((X - 1) Mod 13) = 12 Then
TempDeck(X) = "King"
Else
TempDeck(X) = CStr(1 + ((X - 1) Mod 13))
End If
TempDeck(X) = TempDeck(X) & " of "
If (X - 1) \ 13 = 0 Then
TempDeck(X) = TempDeck(X) & "Spades"
ElseIf (X - 1) \ 13 = 1 Then
TempDeck(X) = TempDeck(X) & "Hearts"
ElseIf (X - 1) \ 13 = 2 Then
TempDeck(X) = TempDeck(X) & "Diamonds"
ElseIf (X - 1) \ 13 = 3 Then
TempDeck(X) = TempDeck(X) & "Clubs"
End If
Next
End If
End If
'Let us shuffle the deck
X = 52
For X = 52 To 1 Step -1
TempInt = Int(X * Rnd + 1)
Deck(X) = TempDeck(TempInt)
TempCard = TempDeck(X)
TempDeck(X) = TempDeck(TempInt)
TempDeck(TempInt) = TempCard
Next
End Sub

Everything is self-contained in this version; just pass it an array
dimensioned between 1 and 52 as in this example use:

Private Sub Command1_Click()
Dim MyDeck(1 To 52) As String
ShuffleDeck MyDeck
Debug.Print MyDeck(1) & ", " & MyDeck(4) & ", " & MyDeck(43)
End Sub
Jul 17 '05 #10
> Private Sub Command1_Click()
Dim MyDeck(1 To 52) As String
ShuffleDeck MyDeck
Debug.Print MyDeck(1) & ", " & MyDeck(4) & ", " & MyDeck(43)
End Sub


I'm not sure why I opted to use the above code originally when I chose to
demo the function. The deck of cards is completely randomized after the
function call, so selecting cards is as simple as iterating through the
array sequentially. Perhaps a better demo code would have been this

Private Sub Command1_Click()
Dim X As Long
Dim MyDeck(1 To 52) As String
ShuffleDeck MyDeck
For X = 1 To 5
Debug.Print MyDeck(X)
Next
End Sub

which, in effect, deals out a random hand of 5 cards.

Rick - MVP
Jul 17 '05 #11

On Wed, 24 Sep 2003, Calum wrote:

When you swap two things you need a temporary**! Why loop to 1000? You
can loop just 52 times if you swap each card with a randomly chosen other.

for(int c=0; c<52; ++c)
{
Card &a = cards[c];
Card &b = cards[rnd(52)]; // For a suitably defined rnd

Card t = a; // Not expensive if Card is 4 bytes or 1 byte
a = b;
b = t;
}
Geez Louise, isn't this a FAQ *somewhere*?
The above algorithm *does* *not* *generate*
*a* *uniform* *distribution*! Get that through
your head! Arrrgh!

** There is also an XOR hack:
a = a^b, b = a^b, a = a^b;
saves a register spill :-)


....at the cost of producing incorrect results
when &a == &b, as is likely with your algorithm
as it stands.

-Arthur

Jul 17 '05 #12

"Mel Wilson" <mw*****@the-wire.com> wrote in message
news:cibc/ks/Kz*******@the-wire.com...
I would go with
For C = 0 to 51
Cards(C) = C
Next C
DeckCount = 52

Function DealOne
If DeckCount < 1
DeckCount = 52 ' simulate reshuffle and keep going
End If
I = Int (Rnd * DeckCount) ' a random place in the deck
DeckCount = DeckCount - 1 ' decrease the card count
X = Cards(I) ' the card to deal
Cards(I) = Cards(DeckCount) ' swap dealt card with last card
Cards(DeckCount) = X
DealOne = X ' return the dealt card
End Function

For I = 0 to 6
RummyHand(I) = DealOne()
Next I
This code probably contains errors -- my Basic is in bad shape

Good Luck. Mel.


I agree. Why not pick randomly from a sequential deck. No one knows the deck
is "not shuffled" and drawing randomly is the same as having a shuffled deck
:)
Jul 17 '05 #13
gb*@email.si (Gorazd Bozic) wrote in news:bk**********@planja.arnes.si:
In article <Xn******************************@204.127.36.1>,
James Rogers <ji**************@att.net> wrote:
...
-- Define the number of cards in a standard deck.
subtype Deck_Index is integer range 1..52;


Just curious -- why not

subtype Deck_Index is integer range Pips'Range * Suits'Range;


The reason is pretty simple. Both Pips and Suits are defined as
enumerated types. In Ada enumerated types are non-numeric discrete
types. There is no multiplication operation defined for any
emumerated type.

Is that not possible? That would be consistent with the Initialize
function:
function Initialize return Card_Deck is
Result : Card_Deck;
Temp_Index : Integer := Deck_Index'First;
begin
for The_Suit in Suits loop
for The_Pip in Pips loop
Result(Temp_Index) := (The_Pip, The_Suit);
Temp_Index := Temp_Index + 1;
end loop;
end loop;
return Result;
end Initialize;

To achieve the equivalent of what you want in Ada I would need to define
Card_Deck as a two dimensional array of something. What would it be an
array of? Since the index values are not numeric types it could not be
an array of integers. It would need to be an array of Suits and Pips,
expressed as an Ada record, which is what a Card is defined to be.
This means that I do not have any advantage in using an indexing
array. I still need to handle multiple copies of each full deck.

The next problem you have is creating random instances of that record
type for your shuffle procedure. The problem gets a lot more complex
and a lot uglier if we follow this path.

Jim Rogers
Jul 17 '05 #14
"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:NA****************@nwrdny01.gnilink.net:


I agree. Why not pick randomly from a sequential deck. No one knows
the deck is "not shuffled" and drawing randomly is the same as having
a shuffled deck
:)


Because that is not efficient.
Every card must have a flag indicating whether or not it has been dealt.
Your random pick will frequently pick a card that has already been dealt,
requiring you to pick another card until you find one that has not yet
been dealt. This problem gets worse as you deal more cards. Dealing the
last card from the deck can be very slow.

The efficient algorithm is:

Assuming the deck array is indexed from 0 through 52:

Current_Index = 0

while Current_Index < 51 loop
swap(Card[Current_Index],
Card[(random * (52 - Current_Index + 1)) + Current_Index];
Current_Index = Current_Index + 1;
end loop;

The above algorithm assumes the swap procedure will exchange the
array values, and that the random function will return a random
number between 0 and 1. It also assumes an implicit conversion
from floating point values to integer values.

Note that the shuffle algorithm marches through the array.
It ensures that every position is swapped with some other
position. It performs N-1 swaps if N is the number of cards
in the deck.

Once the array has been shuffled you only need to keep track
of which index is the next to be dealt. No unnecessary
repeated attempts.

Jim Rogers
Jul 17 '05 #15

"JC" <no************@hotmail.com> wrote in message
news:bk**********@sun-news.laserlink.net...
I want to shuffle a deck of 52 cards.


This is just for fun, since there has been some discussion about "really random"
and stuff.

This code does not attempt to be truly random; instead, it attempts to simulate
what happens when humans shuffle cards. That is, it splits the deck into two
piles, and then drops cards alternately from the two piles. The initial split is
26 +/- 4 in each pile, and each drop is between 1 to 4 cards. The PNorm function
returns more 1's than 4's, but by a crude mechanism, not a real distribution.

The interesting thing is how much order remains after just one shuffle. Don't
expect real randomization from just one shuffle of a deck!

Steve

---this is the form code for a form with a single button called cmdShuffle.

Option Explicit

Private mDeck(1 To 52) As Integer
Private mNewDeck(1 To 52) As Integer
Private mNewCards As Integer

Private Sub Form_Load()
Dim n As Integer

'open the new pack of cards
For n = 1 To 52
mDeck(n) = n
Debug.Print mDeck(n),
Next n
Debug.Print

'seed the random generator
Randomize Timer

End Sub

Private Sub cmdShuffle_Click()
Dim nLeftPile As Integer
Dim nRightPile As Integer
Dim nSplit As Integer
Dim n As Integer

'reset index
mNewCards = 0

'split into approximately two halves
nSplit = 24 + PNorm()
nLeftPile = nSplit
nRightPile = 52 - nLeftPile

'alternately drop some from each pile, till they are all gone
Do Until nLeftPile = 0 And nRightPile = 0
Call DropSome(nLeftPile, 0)
Call DropSome(nRightPile, nSplit)
Loop

'copy the new deck into the main deck
For n = 1 To 52
mDeck(n) = mNewDeck(n)
Debug.Print mDeck(n),
Next n
Debug.Print

End Sub

Private Sub DropSome(ByRef Pile As Integer, ByVal Offset As Integer)
Dim nDrop As Integer
Dim n As Integer

If Pile > 0 Then

'decide how many to drop from this pile
nDrop = PNorm()
If nDrop > Pile Then
nDrop = Pile
End If

'drop each card from the "pile" into the new deck
For n = 1 To nDrop
mNewCards = mNewCards + 1
mNewDeck(mNewCards) = mDeck(Offset + Pile)
Pile = Pile - 1
Next n

End If

End Sub

Private Function PNorm() As Integer
'a very pseudo normal random number - more like a triangle, actually...

Dim nNum As Integer

'random int from 0 to 9
nNum = CInt(Rnd * 10)

If nNum < 4 Then
'(0 to 3) average 40% of these
PNorm = 1

ElseIf nNum < 7 Then
'(4 to 6) average 30% of these
PNorm = 2

ElseIf nNum < 9 Then
'(7 to 8) average 20% of these
PNorm = 3

Else
'(9) average 10% of these
PNorm = 4
End If

End Function
Jul 17 '05 #16
Arthur J. O'Dwyer wrote:
On Wed, 24 Sep 2003, Calum wrote:
When you swap two things you need a temporary**! Why loop to 1000? You
can loop just 52 times if you swap each card with a randomly chosen other.

for(int c=0; c<52; ++c)
{
Card &a = cards[c];
Card &b = cards[rnd(52)]; // For a suitably defined rnd

Card t = a; // Not expensive if Card is 4 bytes or 1 byte
a = b;
b = t;
}

Geez Louise, isn't this a FAQ *somewhere*?
The above algorithm *does* *not* *generate*
*a* *uniform* *distribution*! Get that through
your head! Arrrgh!


Feel better now?

Oh, ooops, did I write rnd(52) instead of rnd(c)? Must have gotten
corrupted on the news server somehow, cough splutter...

Yes the above algorithm is ever-so-slightly biased, linear-congruential
RNG notwithstanding. *Hangs head in shame* :-)
** There is also an XOR hack:
a = a^b, b = a^b, a = a^b;
saves a register spill :-)

....at the cost of producing incorrect results
when &a == &b, as is likely with your algorithm
as it stands.


Good point, but I didn't use it for this algorithm did I? It was just
in case someone took issue with the word "need". I thought that little
snippet was a bit of fun, oh dear.

Calum

Jul 17 '05 #17

"James Rogers" <ji**************@att.net> wrote in message
news:Xn******************************@204.127.36.1 ...
"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:NA****************@nwrdny01.gnilink.net:


I agree. Why not pick randomly from a sequential deck. No one knows
the deck is "not shuffled" and drawing randomly is the same as having
a shuffled deck
:)
Because that is not efficient.
Every card must have a flag indicating whether or not it has been dealt.
Your random pick will frequently pick a card that has already been dealt,
requiring you to pick another card until you find one that has not yet
been dealt.


Jim.. if you draw the card you simply zero the array. If it's zero it's not
pickable.
This problem gets worse as you deal more cards. Dealing the last card from the deck can be very slow.


Well.. you either haven't been programming for very long or you have no idea
about processor power. On a very old 166 Pentium 1, VB can loop 50,000
(fifty thousand times) executing a random statement plus two IF then
statements in less than 8/10 eight tenth of a second. Slow ? Yeah sure not
nano second.

Why don't you try this on your PC.. if it takes more than three-tenth of a
second, you have a very slow PC :)

Private Sub Command1_Click()
Dim k, x
Label1.Caption = Timer
For x = 1 To 50000
k = Int((52 * Rnd) + 1)
Next x
Label2.Caption = Timer
Label3.Caption = Val(Label2.Caption) - Val(Label1.Caption)
End Sub

Jul 17 '05 #18
"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:WQ*****************@nwrdny01.gnilink.net:

"James Rogers" <ji**************@att.net> wrote in message
news:Xn******************************@204.127.36.1 ...
"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:NA****************@nwrdny01.gnilink.net:
>
>
> I agree. Why not pick randomly from a sequential deck. No one knows
> the deck is "not shuffled" and drawing randomly is the same as
> having a shuffled deck
>:)
Because that is not efficient.
Every card must have a flag indicating whether or not it has been
dealt. Your random pick will frequently pick a card that has already
been dealt, requiring you to pick another card until you find one
that has not yet been dealt.


Jim.. if you draw the card you simply zero the array. If it's zero
it's not pickable.


That is yet another inefficiency. Did you ever study algorithms?
This problem gets worse as you deal more cards. Dealing the last card from the deck can be very slow.


Well.. you either haven't been programming for very long or you have
no idea about processor power. On a very old 166 Pentium 1, VB can
loop 50,000 (fifty thousand times) executing a random statement plus
two IF then statements in less than 8/10 eight tenth of a second. Slow
? Yeah sure not nano second.


Wrong on both counts. I have been programming since 1972.
I do understand the speed of a 3 GHz processor.

Obviously you come from the school of thought that creates fatware.
Do you really believe that efficiency is unnecessary if a processor
is fast? 8/10 of a second on a multi-GHz processor is an eternity.
Why do you want to buy a fast processor then force it to execute
slow code? That simply wastes the resources of the processor.

Why don't you try this on your PC.. if it takes more than three-tenth
of a second, you have a very slow PC :)

<snip irrelevant example>

0.3 seconds. Let's see, that is 300000 nanoseconds, or 900000 cycles for
a 3GHz processor.

An algorithm that takes 300000 cycles (0.1 seconds) is very inefficient
compared to another algorithm that achieves the same goals yet takes only
5769 cycles (0.002 seconds). Why are you satisfied with making your
fast processor behave as though it was operating at only 2% of its
actual speed? You are effectively making your 3GHz processor run at 58
MHz. By today's standards that is terrible performance.

If you want to benefit from all your system's performance you cannot
run grossly inefficient software on the machine. Overall system performance
is achieved through the combined effects of fast hardware and efficient
software. Just as slow hardware will reduce your performance, inefficient
software will also reduce performance. It is a waste of money to buy
fast hardware so that you can run inefficient software.

You buy fast hardware because performance matters. You then claim that it
is acceptable to run slow software because performance does not matter.
I do not think such a position is well reasoned.

Jim Rogers
Jul 17 '05 #19
In article <Xn****************************@204.127.36.1>,
James Rogers <ji**************@att.net> wrote:

The reason is pretty simple. Both Pips and Suits are defined as
enumerated types. In Ada enumerated types are non-numeric discrete
types. There is no multiplication operation defined for any
emumerated type.


This is not what I meant. If you can get the number of elements in an
enumerated type (I assume the attribute is called Range):

Suits'Range = 4
Pips'Range = 13

So if you take these two numbers, surely you can multiply them and get
52 as a result. So instead of using a hardcoded number, you would use
the actual number of all possible cards as defined by those two
types. That would allow you to change the definition of Pips or Suits
and avoid having to recalculate by hand and change another constant in
a program.

I guess if you make a mistake in a constant, the Initialize function
will generate an exception. If you use Suits'Range * Pips'Range, it will
not as the types will be consistant with the range of Deck_Index.
--
Gorazd Bozic <gb*@email.si>
Jul 17 '05 #20
gb*@email.si (Gorazd Bozic) wrote in news:bl**********@planja.arnes.si:
In article <Xn****************************@204.127.36.1>,
James Rogers <ji**************@att.net> wrote:

The reason is pretty simple. Both Pips and Suits are defined as
enumerated types. In Ada enumerated types are non-numeric discrete
types. There is no multiplication operation defined for any
emumerated type.


This is not what I meant. If you can get the number of elements in an
enumerated type (I assume the attribute is called Range):

Suits'Range = 4
Pips'Range = 13

So if you take these two numbers, surely you can multiply them and get
52 as a result. So instead of using a hardcoded number, you would use
the actual number of all possible cards as defined by those two
types. That would allow you to change the definition of Pips or Suits
and avoid having to recalculate by hand and change another constant in
a program.

I guess if you make a mistake in a constant, the Initialize function
will generate an exception. If you use Suits'Range * Pips'Range, it will
not as the types will be consistant with the range of Deck_Index.


I see what you mean. Your assumption of the Ada syntax is reasonable
but not quite accurate.

In Ada, the Range attribute evaluates to the full range of values, not
the number of values, for a type. In other words, it evaluates to a
list of all the valid values for a type.

Every Ada enumerated type has several automatically defined attributes.
Range is one. Another is Pos. The Pos attribute returns the position
number of an enumeration value. There are also the attributes First
and Last which evaluate to the lowest and highest valid value for the
enumerated type.

Using this information, I could have written my code as follows.

Suits_Length : constant := Suits'Pos(Suits'Last) -
Suits'Pos(Suits'First) + 1;
Pips_Length : constant := Pips'Pos(Pips'Last) -
Pips'Pos(Pips'First) + 1;

type Deck_Index is range (1..Suits_Length * Pips_Length);

This would have caused the compiler to calculate the values,
eliminating any possible counting error on my part. If I wanted
to define the card deck in a generic package I would have been
forced to use this approach. I was taking a short cut.

You are correct that this approach is generally stronger.

Jim Rogers
Jul 17 '05 #21
I'll be happy to send anyone my Ada textbooks...Don't need them anymore :-D
Jul 17 '05 #22

"James Rogers" <ji**************@att.net> wrote in message
news:Xn******************************@204.127.36.1 ...
"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:WQ*****************@nwrdny01.gnilink.net:

"James Rogers" <ji**************@att.net> wrote in message
news:Xn******************************@204.127.36.1 ...
"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:NA****************@nwrdny01.gnilink.net:

>
>
> I agree. Why not pick randomly from a sequential deck. No one knows
> the deck is "not shuffled" and drawing randomly is the same as
> having a shuffled deck
>:)

Because that is not efficient.
Every card must have a flag indicating whether or not it has been
dealt. Your random pick will frequently pick a card that has already
been dealt, requiring you to pick another card until you find one
that has not yet been dealt.
Jim.. if you draw the card you simply zero the array. If it's zero
it's not pickable.


That is yet another inefficiency. Did you ever study algorithms?
This problem gets worse as you deal more cards. Dealing the

last card from the deck can be very slow.


Well.. you either haven't been programming for very long or you have
no idea about processor power. On a very old 166 Pentium 1, VB can
loop 50,000 (fifty thousand times) executing a random statement plus
two IF then statements in less than 8/10 eight tenth of a second. Slow
? Yeah sure not nano second.


Wrong on both counts. I have been programming since 1972.
I do understand the speed of a 3 GHz processor.

Obviously you come from the school of thought that creates fatware.
Do you really believe that efficiency is unnecessary if a processor
is fast? 8/10 of a second on a multi-GHz processor is an eternity.
Why do you want to buy a fast processor then force it to execute
slow code? That simply wastes the resources of the processor.

Why don't you try this on your PC.. if it takes more than three-tenth
of a second, you have a very slow PC :)

<snip irrelevant example>

0.3 seconds. Let's see, that is 300000 nanoseconds, or 900000 cycles for
a 3GHz processor.

An algorithm that takes 300000 cycles (0.1 seconds) is very inefficient
compared to another algorithm that achieves the same goals yet takes only
5769 cycles (0.002 seconds). Why are you satisfied with making your
fast processor behave as though it was operating at only 2% of its
actual speed? You are effectively making your 3GHz processor run at 58
MHz. By today's standards that is terrible performance.

If you want to benefit from all your system's performance you cannot
run grossly inefficient software on the machine. Overall system

performance is achieved through the combined effects of fast hardware and efficient
software. Just as slow hardware will reduce your performance, inefficient
software will also reduce performance. It is a waste of money to buy
fast hardware so that you can run inefficient software.

You buy fast hardware because performance matters. You then claim that it
is acceptable to run slow software because performance does not matter.
I do not think such a position is well reasoned.

Jim Rogers


You wanted to create a flag to indicate that a card is picked. I said,
that's not necessary, simply zero the array. So *who* is being inefficient?
Study algorithms? Why.. if you did, you wouldn't have used a flag. If you
started programming in 1972, you would understand that memory is precious.
An array of flags is a waste of memory.

You would also know that computing power is premium. Remember checking your
card deck after being punched? Remember only having 32K of RAM and you have
to run a Cobol program while compiling RPG? Playing computer so that you
only need to compile once? Those days are gone. Yes, we can be wastefull
now.

My point is exactly that. Just because a method is not "elegant" it doesn't
mean that it doesn't work. I know several elegant "algorithm savvy" way of
shuffling a card deck but that wasn't as simple as the one Mel Wilson
mentioned, and I agree with him.

These were your exact words about speed "Dealing the last card from the deck
can be very slow" when you mention *slow*, it is from a user point of
view -NOT- processor point of view as you wormed out. All I was saying is,
you are wrong, it won't be slow (from the user perspective). Who cares about
CPU wasted cycles?

If you worry about CPU wasted cycle, and "fat programming" (huh? If you
compile my method and yours, you will see whose code is fatter :), since you
are an old timer like me, I am sure we can whip up a very memory and speed
efficient shuffling routine. Not in VB, but in Assembly.

I have no further comments on this subject.
Jul 17 '05 #23
Raoul Watson wrote:
"James Rogers" <ji**************@att.net> wrote in message
news:Xn******************************@204.127.36.1 ...
"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:WQ*****************@nwrdny01.gnilink.net:

"James Rogers" <ji**************@att.net> wrote in message
news:Xn******************************@204.127.3 6.1...

"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in
news:NA****************@nwrdny01.gnilink.net :
>
>I agree. Why not pick randomly from a sequential deck. No one knows
>the deck is "not shuffled" and drawing randomly is the same as
>having a shuffled deck
>:)

Because that is not efficient.
Every card must have a flag indicating whether or not it has been
dealt. Your random pick will frequently pick a card that has already
been dealt, requiring you to pick another card until you find one
that has not yet been dealt.

Jim.. if you draw the card you simply zero the array. If it's zero
it's not pickable.


That is yet another inefficiency. Did you ever study algorithms?

This problem gets worse as you deal more cards. Dealing the

last card from the deck can be very slow.

Well.. you either haven't been programming for very long or you have
no idea about processor power. On a very old 166 Pentium 1, VB can
loop 50,000 (fifty thousand times) executing a random statement plus
two IF then statements in less than 8/10 eight tenth of a second. Slow
? Yeah sure not nano second.


Wrong on both counts. I have been programming since 1972.
I do understand the speed of a 3 GHz processor.

Obviously you come from the school of thought that creates fatware.
Do you really believe that efficiency is unnecessary if a processor
is fast? 8/10 of a second on a multi-GHz processor is an eternity.
Why do you want to buy a fast processor then force it to execute
slow code? That simply wastes the resources of the processor.

Why don't you try this on your PC.. if it takes more than three-tenth
of a second, you have a very slow PC :)


<snip irrelevant example>

0.3 seconds. Let's see, that is 300000 nanoseconds, or 900000 cycles for
a 3GHz processor.

An algorithm that takes 300000 cycles (0.1 seconds) is very inefficient
compared to another algorithm that achieves the same goals yet takes only
5769 cycles (0.002 seconds). Why are you satisfied with making your
fast processor behave as though it was operating at only 2% of its
actual speed? You are effectively making your 3GHz processor run at 58
MHz. By today's standards that is terrible performance.

If you want to benefit from all your system's performance you cannot
run grossly inefficient software on the machine. Overall system


performance
is achieved through the combined effects of fast hardware and efficient
software. Just as slow hardware will reduce your performance, inefficient
software will also reduce performance. It is a waste of money to buy
fast hardware so that you can run inefficient software.

You buy fast hardware because performance matters. You then claim that it
is acceptable to run slow software because performance does not matter.
I do not think such a position is well reasoned.

Jim Rogers

You wanted to create a flag to indicate that a card is picked. I said,
that's not necessary, simply zero the array. So *who* is being inefficient?
Study algorithms? Why.. if you did, you wouldn't have used a flag. If you
started programming in 1972, you would understand that memory is precious.
An array of flags is a waste of memory.

You would also know that computing power is premium. Remember checking your
card deck after being punched? Remember only having 32K of RAM and you have
to run a Cobol program while compiling RPG? Playing computer so that you
only need to compile once? Those days are gone. Yes, we can be wastefull
now.

My point is exactly that. Just because a method is not "elegant" it doesn't
mean that it doesn't work. I know several elegant "algorithm savvy" way of
shuffling a card deck but that wasn't as simple as the one Mel Wilson
mentioned, and I agree with him.

These were your exact words about speed "Dealing the last card from the deck
can be very slow" when you mention *slow*, it is from a user point of
view -NOT- processor point of view as you wormed out. All I was saying is,
you are wrong, it won't be slow (from the user perspective). Who cares about
CPU wasted cycles?

If you worry about CPU wasted cycle, and "fat programming" (huh? If you
compile my method and yours, you will see whose code is fatter :), since you
are an old timer like me, I am sure we can whip up a very memory and speed
efficient shuffling routine. Not in VB, but in Assembly.

I have no further comments on this subject.

Despite your saying you have no further comments, I must interject.

A single inelegant (read, slow or inneficient) algorithm is not a
killing point. However, when one programs sloppily and learns to use
inelegant algorithms on a regular basis, their software becomes bloated.
One of our previous programmers learned this way; and thus he is
previous and not current. At one point he created a class to hold a
single variable.

My boss programs inelegantly. His mindset is simply that "processors
are getting faster, so I can program less efficiently.". While his
programs parse data and run for days, mine finish overnight. Why?
Sure, when he runs one or two iterations of his inelegant code - he may
not notice a speed decline. However when he iterates fifty thousand
times, it is quickly noticed (or rather slowly noticed ;p).

Regarding the topic of "memory consumption"; I haven't followed this
thread carefully, so I'm not sure what he meant by "flag"... memory is
much more volatile and inconsequential than cpu speed [1]. So long as
you clear out your memory for each function run (read: your function
isn't recursive); if it takes an extra five megabytes of memory, so
what? The program won't theoretically run any slower - as far as I see.
Mayhaps a small amount, constructing the flag - but that may be
miniscule compared to zeroing the array [2].

And I fully agree with your final paragraph. :) Indeed, if you are
programming in VB - your mind is not quite set on being effecient with
the cpu; rather with being effecient with your own time.

*breaths and waits to be set upon by wolves*

[1] When dealing with small amounts. Larger amounts, that may cause
swapping or machine lag - sure, then care. But the extra 5k that may be
created by an extra array...

[2] That is, rather than comparing it to doing nothing - compare it to
zeroing the array; not that zeroing the array is slower.

--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Jordan T. Cox
Programmer, IT Administrator, Tech Support
Geronimo Development Corporation
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

Jul 17 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: DaveLessnau | last post by:
I'm trying to learn C on my own and, apparently, my brain went on vacation somewhere. I just can't figure out how to parse the following function call: "void fillDeck( Card * const wDeck, const...
6
by: CaseyB | last post by:
If I wanted to create a game like Solitaire that would first randomly shuffle a deck of cards, I figured out that all I had to use is the Random() class or rnd and make sure I use the Randomize...
4
by: Pratik | last post by:
For the time being, I'm doing a simple swap method for my deck of cards using the random number generator in cstdlib. I've created a dynamic array of a type class Card. When I go to shuffle it and...
4
by: tvance929 | last post by:
Hey everyone, I created a theDeck class that creates a 52 card int List. Inside of this class I have a ShuffleCards method. I simply want 2 seperate decks that I can then shuffle and...
8
by: l1nuxxx | last post by:
I have a file well call file.pl. It's a card sorting program. I need to create a lib fuction with part of the original file that shuffles the deck of cards. After it shuffles the first deck and deals...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.