473,804 Members | 3,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Shuffle a deck of cards

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
that represents that deck. That sounds like some kind of math algorithm and
perhaps maybe there's a dll or library out there for assigning a number to
each of the number of possibilities but was unable to find anything for use
on dotnet. Any suggestions?
thanks

Casey
Nov 20 '05 #1
6 6223
Hi Casey,

Randomize can take an optional parameter that is used as the seed for the
random number. By default, if no seed is passed, the system time is used
for the seed. If you seed the random number with a specific number then you
will always get the same sequence of numbers when you repeatedly call the
Rnd function...

\\\
Randomize(42949 67296)
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExi st.net> wrote in message
news:ec******** ******@tk2msftn gp13.phx.gbl...
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
that represents that deck. That sounds like some kind of math algorithm and perhaps maybe there's a dll or library out there for assigning a number to
each of the number of possibilities but was unable to find anything for use on dotnet. Any suggestions?
thanks

Casey

Nov 20 '05 #2
Hi Casey,

Randomize can take an optional parameter that is used as the seed for the
random number. By default, if no seed is passed, the system time is used
for the seed. If you seed the random number with a specific number then you
will always get the same sequence of numbers when you repeatedly call the
Rnd function...

\\\
Randomize(42949 67296)
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExi st.net> wrote in message
news:ec******** ******@tk2msftn gp13.phx.gbl...
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
that represents that deck. That sounds like some kind of math algorithm and perhaps maybe there's a dll or library out there for assigning a number to
each of the number of possibilities but was unable to find anything for use on dotnet. Any suggestions?
thanks

Casey

Nov 20 '05 #3
Thanks Gary for taken the time for your help,
that puts me in the right direction.

I just thought I share 2 observations:

1. When running that code I get a repeat of some numbers.
Example:
39 10 21 30 11 42 36 28 15 4 39
2. If I put the code inside a Sub and then run the same routine
within a running session you get a different set a numbers.
Example
39 10 21 30 11 42 36 28 15 4 39
32 37 10 48 42 49 19 3 38 17 50
Just for fun I tried running the sample code in the help file under
'Randomize' and added the 'For-next' to run it 6 times

Dim MyValue As Integer
Randomize
for x = 1 to 6
MyValue = CInt(Int((6 * Rnd()) + 1))
Console.WriteLi ne(MyValue.toSt ring)
next x

In this case I get some duplicates there too.
I think Randomize (with/without number in arguement) just
decreases duplicate numbers but does not prevent it entirely.
I'll just have to make a routine that removes the duplicates.

Casey

----- Original Message -----
From: "Gary Milton" <an*******@disc ussions.microso ft.com>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
Sent: Saturday, April 10, 2004 8:44 AM
Subject: Re: How to Shuffle a deck of cards

Hi Casey,

Randomize can take an optional parameter that is used as the seed for the
random number. By default, if no seed is passed, the system time is used
for the seed. If you seed the random number with a specific number then you will always get the same sequence of numbers when you repeatedly call the
Rnd function...

\\\
Randomize(42949 67296)
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExi st.net> wrote in message
news:ec******** ******@tk2msftn gp13.phx.gbl...
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
that represents that deck. That sounds like some kind of math algorithm

and
perhaps maybe there's a dll or library out there for assigning a number to each of the number of possibilities but was unable to find anything for

use
on dotnet. Any suggestions?
thanks

Casey


Nov 20 '05 #4
Thanks Gary for taken the time for your help,
that puts me in the right direction.

I just thought I share 2 observations:

1. When running that code I get a repeat of some numbers.
Example:
39 10 21 30 11 42 36 28 15 4 39
2. If I put the code inside a Sub and then run the same routine
within a running session you get a different set a numbers.
Example
39 10 21 30 11 42 36 28 15 4 39
32 37 10 48 42 49 19 3 38 17 50
Just for fun I tried running the sample code in the help file under
'Randomize' and added the 'For-next' to run it 6 times

Dim MyValue As Integer
Randomize
for x = 1 to 6
MyValue = CInt(Int((6 * Rnd()) + 1))
Console.WriteLi ne(MyValue.toSt ring)
next x

In this case I get some duplicates there too.
I think Randomize (with/without number in arguement) just
decreases duplicate numbers but does not prevent it entirely.
I'll just have to make a routine that removes the duplicates.

Casey

----- Original Message -----
From: "Gary Milton" <an*******@disc ussions.microso ft.com>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
Sent: Saturday, April 10, 2004 8:44 AM
Subject: Re: How to Shuffle a deck of cards

Hi Casey,

Randomize can take an optional parameter that is used as the seed for the
random number. By default, if no seed is passed, the system time is used
for the seed. If you seed the random number with a specific number then you will always get the same sequence of numbers when you repeatedly call the
Rnd function...

\\\
Randomize(42949 67296)
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
Console.WriteLi ne(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExi st.net> wrote in message
news:ec******** ******@tk2msftn gp13.phx.gbl...
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
that represents that deck. That sounds like some kind of math algorithm

and
perhaps maybe there's a dll or library out there for assigning a number to each of the number of possibilities but was unable to find anything for

use
on dotnet. Any suggestions?
thanks

Casey


Nov 20 '05 #5
What's wrong with duplicate numbers? I don't think you understand the
problem that you're working on. The Rnd function is used to swap cards
in a card array. It doesn't matter if the Rnd function produces
duplicate numbers. Here's a card shuffling routine for you to try:

Dim deck(51) as Integer
Dim temp as Integer
Dim k as Integer
Dim j as Integer

'--- For game #767220
Randomize(76722 0)

'-- Initialize the deck
For i = 0 to 51
deck(i) = i
Next

For j = 0 To 51
k = (Int(Rnd * 51) + 1)
'Let's swap positions
temp = m_Deck(j)
m_Deck(j) = m_Deck(k)
m_Deck(k) = temp
Next

You can use Randomize to select the game number:

Randomize(19844 9); for game 198449
Randomize(512); for game 512
Randomize(98539 5); for game 985395

and so on.
CaseyB wrote:
Thanks Gary for taken the time for your help,
that puts me in the right direction.

I just thought I share 2 observations:

1. When running that code I get a repeat of some numbers.
Example:
39 10 21 30 11 42 36 28 15 4 39
2. If I put the code inside a Sub and then run the same routine
within a running session you get a different set a numbers.
Example
39 10 21 30 11 42 36 28 15 4 39
32 37 10 48 42 49 19 3 38 17 50
Just for fun I tried running the sample code in the help file under
'Randomize' and added the 'For-next' to run it 6 times

Dim MyValue As Integer
Randomize
for x = 1 to 6
MyValue = CInt(Int((6 * Rnd()) + 1))
Console.WriteLi ne(MyValue.toSt ring)
next x

In this case I get some duplicates there too.
I think Randomize (with/without number in arguement) just
decreases duplicate numbers but does not prevent it entirely.
I'll just have to make a routine that removes the duplicates.

Casey

----- Original Message -----
From: "Gary Milton" <an*******@disc ussions.microso ft.com>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
Sent: Saturday, April 10, 2004 8:44 AM
Subject: Re: How to Shuffle a deck of cards
Hi Casey,

Randomize can take an optional parameter that is used as the seed for the
random number. By default, if no seed is passed, the system time is used
for the seed. If you seed the random number with a specific number then


you
will always get the same sequence of numbers when you repeatedly call the
Rnd function...

\\\
Randomize(429 4967296)
Console.Write Line(Int((52 * Rnd()) + 1))
Console.Write Line(Int((52 * Rnd()) + 1))
Console.Write Line(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExi st.net> wrote in message
news:ec****** ********@tk2msf tngp13.phx.gbl. ..
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
that represents that deck. That sounds like some kind of math algorithm


and
perhaps maybe there's a dll or library out there for assigning a number
to
each of the number of possibilities but was unable to find anything for


use
on dotnet. Any suggestions?
thanks

Casey



Nov 20 '05 #6
What's wrong with duplicate numbers? I don't think you understand the
problem that you're working on. The Rnd function is used to swap cards
in a card array. It doesn't matter if the Rnd function produces
duplicate numbers. Here's a card shuffling routine for you to try:

Dim deck(51) as Integer
Dim temp as Integer
Dim k as Integer
Dim j as Integer

'--- For game #767220
Randomize(76722 0)

'-- Initialize the deck
For i = 0 to 51
deck(i) = i
Next

For j = 0 To 51
k = (Int(Rnd * 51) + 1)
'Let's swap positions
temp = m_Deck(j)
m_Deck(j) = m_Deck(k)
m_Deck(k) = temp
Next

You can use Randomize to select the game number:

Randomize(19844 9); for game 198449
Randomize(512); for game 512
Randomize(98539 5); for game 985395

and so on.
CaseyB wrote:
Thanks Gary for taken the time for your help,
that puts me in the right direction.

I just thought I share 2 observations:

1. When running that code I get a repeat of some numbers.
Example:
39 10 21 30 11 42 36 28 15 4 39
2. If I put the code inside a Sub and then run the same routine
within a running session you get a different set a numbers.
Example
39 10 21 30 11 42 36 28 15 4 39
32 37 10 48 42 49 19 3 38 17 50
Just for fun I tried running the sample code in the help file under
'Randomize' and added the 'For-next' to run it 6 times

Dim MyValue As Integer
Randomize
for x = 1 to 6
MyValue = CInt(Int((6 * Rnd()) + 1))
Console.WriteLi ne(MyValue.toSt ring)
next x

In this case I get some duplicates there too.
I think Randomize (with/without number in arguement) just
decreases duplicate numbers but does not prevent it entirely.
I'll just have to make a routine that removes the duplicates.

Casey

----- Original Message -----
From: "Gary Milton" <an*******@disc ussions.microso ft.com>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
Sent: Saturday, April 10, 2004 8:44 AM
Subject: Re: How to Shuffle a deck of cards
Hi Casey,

Randomize can take an optional parameter that is used as the seed for the
random number. By default, if no seed is passed, the system time is used
for the seed. If you seed the random number with a specific number then


you
will always get the same sequence of numbers when you repeatedly call the
Rnd function...

\\\
Randomize(429 4967296)
Console.Write Line(Int((52 * Rnd()) + 1))
Console.Write Line(Int((52 * Rnd()) + 1))
Console.Write Line(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExi st.net> wrote in message
news:ec****** ********@tk2msf tngp13.phx.gbl. ..
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
that represents that deck. That sounds like some kind of math algorithm


and
perhaps maybe there's a dll or library out there for assigning a number
to
each of the number of possibilities but was unable to find anything for


use
on dotnet. Any suggestions?
thanks

Casey



Nov 20 '05 #7

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

Similar topics

23
12963
by: JC | last post by:
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
6
343
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
9194
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
5058
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
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
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...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
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
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6858
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
5527
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.