How to Shuffle a deck of cards

C

CaseyB

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
 
G

Gary Milton

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
 
C

CaseyB

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" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
Sent: Saturday, April 10, 2004 8:44 AM
Subject: Re: How to Shuffle a deck of cards
 
Y

yEaH rIgHt

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top