473,237 Members | 1,094 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,237 software developers and data experts.

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 6134
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(4294967296)
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExist.net> wrote in message
news:ec**************@tk2msftngp13.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(4294967296)
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExist.net> wrote in message
news:ec**************@tk2msftngp13.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.WriteLine(MyValue.toString)
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*******@discussions.microsoft.com>
Newsgroups: microsoft.public.dotnet.languages.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(4294967296)
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExist.net> wrote in message
news:ec**************@tk2msftngp13.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.WriteLine(MyValue.toString)
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*******@discussions.microsoft.com>
Newsgroups: microsoft.public.dotnet.languages.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(4294967296)
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExist.net> wrote in message
news:ec**************@tk2msftngp13.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(767220)

'-- 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(198449); for game 198449
Randomize(512); for game 512
Randomize(985395); 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.WriteLine(MyValue.toString)
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*******@discussions.microsoft.com>
Newsgroups: microsoft.public.dotnet.languages.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(4294967296)
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExist.net> wrote in message
news:ec**************@tk2msftngp13.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(767220)

'-- 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(198449); for game 198449
Randomize(512); for game 512
Randomize(985395); 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.WriteLine(MyValue.toString)
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*******@discussions.microsoft.com>
Newsgroups: microsoft.public.dotnet.languages.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(4294967296)
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
Console.WriteLine(Int((52 * Rnd()) + 1))
///

HTH,
Gary

"CaseyB" <ca*****@NotExist.net> wrote in message
news:ec**************@tk2msftngp13.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
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...
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.