473,830 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
23 12965
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'L ast) -
Pips'Pos(Pips'F irst) + 1;

type Deck_Index is range (1..Suits_Lengt h * 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*****@Intell igenCIA.com> wrote in
news:WQ******** *********@nwrdn y01.gnilink.net :

"James Rogers" <ji************ **@att.net> wrote in message
news:Xn******** *************** *******@204.127 .36.1...
"Raoul Watson" <Wa*****@Intell igenCIA.com> wrote in
news:NA******** ********@nwrdny 01.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*****@Intell igenCIA.com> wrote in
news:WQ****** ***********@nwr dny01.gnilink.n et:

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

"Raoul Watson" <Wa*****@Intell igenCIA.com> wrote in
news:NA**** ************@nw rdny01.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
2245
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 char * wFace, const char * wSuit)" Card is aliased before that call with: "typedef struct card Card"
6
6225
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 function so as not to get the same card twice. BUT I noticed that some Solitaire games allow you to select one of the 4,294,967,296 possibilities in a 52 card deck. I want to do something similar, after I shuffle the deck I want to show the number...
4
9196
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 then later print the shuffled deck, some of the values overlap -- I can't figure out why. Here's my code: void shuffle() { for (counter = 0; counter < 120; counter++) {
4
1502
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 then count a few cards out of each deck.
8
5059
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 a hand of cards I need it to call the shuffling function again before dealing another hand. I call the lib function file file-lib.pl. here's the contents i have for file.pl #!/usr/bin/perl require 'file-lib.pl'; my @startingdeck = ("A...
0
10774
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10526
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9315
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7746
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.