Cutting a deck of cards

R

RVic

Suppose I have a deck of cards, and I shuffle them

import random
cards = []
decks = 6
cards = list(range(13 * 4 * decks))
random.shuffle(cards)

So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array.

For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie
 
M

MRAB

Suppose I have a deck of cards, and I shuffle them

import random
cards = []
decks = 6
cards = list(range(13 * 4 * decks))
random.shuffle(cards)

So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array.

For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie
The list from its start up to, but excluding, index 'i' is cards[ : i],
and the list from index 'i' to its end is cards[i : ].

Now concatenate them those slices.
 
K

Kamlesh Mutha

I guess, you will have to use list slicing mechanism to achieve the desired
result.

Hope this helps,

Cheers,
Kamlesh





Suppose I have a deck of cards, and I shuffle them

import random
cards = []
decks = 6
cards = list(range(13 * 4 * decks))
random.shuffle(cards)

So now I have an array of cards. I would like to cut these cards at some
random point (between 1 and 13 * 4 * decks - 1, moving the lower half of
that to the top half of the cards array.

For some reason, I can't see how this can be done (I know that it must be
a simple line or two in Python, but I am really stuck here). Anyone have
any direction they can give me on this? Thanks, RVic, python newbie
 
C

Carlos Nepomuceno

----------------------------------------
Date: Sun, 26 May 2013 10:52:14 -0700
Subject: Cutting a deck of cards
From: (e-mail address removed)
To: (e-mail address removed)

Suppose I have a deck of cards, and I shuffle them

import random
cards = []
decks = 6
cards = list(range(13 * 4 * decks))
random.shuffle(cards)

So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array.

For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie


list(range(13 * 4 * decks)) == range(13 * 4 * decks)

;)
 
R

RVic

Ah, brilliant -- yes, this is so much more elegant in Python:

#now cut the cards
x = random.randrange(2,range(13 * 4 * decks))
cards = cards[x:]+cards[:x]
 
R

Roy Smith

RVic said:
Suppose I have a deck of cards, and I shuffle them

import random
cards = []
decks = 6
cards = list(range(13 * 4 * decks))
random.shuffle(cards)

So now I have an array of cards. I would like to cut these cards at some
random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that
to the top half of the cards array.

For some reason, I can't see how this can be done (I know that it must be a
simple line or two in Python, but I am really stuck here). Anyone have any
direction they can give me on this? Thanks, RVic, python newbie

import random
i = random.randrange(len(cards))
cut1 = cards[:i]
cut2 = cards[i:]

I haven't thought too much about the boundary conditions, but that's the
general idea.
 
C

Carlos Nepomuceno

----------------------------------------
From: (e-mail address removed) [...]
Not in Python3.xFalse

Adiaý
Marc


What does "list(range(13 * 4 * decks))" returns in Python 3?
 
T

Terry Jan Reedy

----------------------------------------
From: (e-mail address removed) [...]
Not in Python3.x
decks = 6
list(range(13 * 4 * decks)) == range(13 * 4 * decks)
False

Adiaý
Marc


What does "list(range(13 * 4 * decks))" returns in Python 3?

A list, obviously. What you should ask is what range returns in Python
3, and you should install python 3 and try it, and list its attributes.
 
M

Mark Lawrence

----------------------------------------
Date: Sun, 26 May 2013 10:52:14 -0700
Subject: Cutting a deck of cards
From: (e-mail address removed)
To: (e-mail address removed)

Suppose I have a deck of cards, and I shuffle them

import random
cards = []
decks = 6
cards = list(range(13 * 4 * decks))
random.shuffle(cards)

So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array.

For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie


list(range(13 * 4 * decks)) == range(13 * 4 * decks)

;)

Wrong if you're using Python 3 :(
 
C

Carlos Nepomuceno

----------------------------------------
To: (e-mail address removed)
From: (e-mail address removed) [...]
Wrong if you're using Python 3 :(

--
If you're using GoogleCrap™ please read this
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence


Thanks guys! I've been delaying my dive into Python 3 (because I don't needit for now) but I'd like to run some code just to learn how different it is from Python 2 and even other Python flavors.

So, I'd like to know if it's possible to have multiple Python installations on the same machine (Windows 7 in my particular case) without messing one with each other. What care must I take not to mess up with them?

I've just found this[1] awesome service, but ridiculously it's PHP powered!!! lol Come on!!!

Why there aren't more Python powered websites available? What's the catch?


[1] http://www.compileonline.com/execute_python3_online.php
 
C

Chris Angelico

Thanks guys! I've been delaying my dive into Python 3 (because I don't need it for now) but I'd like to run some code just to learn how different it is from Python 2 and even other Python flavors.

So, I'd like to know if it's possible to have multiple Python installations on the same machine (Windows 7 in my particular case) without messing one with each other. What care must I take not to mess up with them?

Easy. Just grab the standard installer and hit it. You'll get two
separate directories (or more; I have \Python26, \Python27, \Python32,
\Python33 on this box), and you can run whichever you want. The one
thing to take care of is .py associations; I haven't actually done it
(on here, all I actually do is IDLE, pretty much), but as of 3.3, you
should be able to use a Unix-style shebang to indicate which Python
you want to invoke.

ChrisA
 
C

Carlos Nepomuceno

----------------------------------------
Date: Mon, 27 May 2013 08:42:56 +1000
Subject: Re: Cutting a deck of cards
From: (e-mail address removed) [...]
Easy. Just grab the standard installer and hit it. You'll get two
separate directories (or more; I have \Python26, \Python27, \Python32,
\Python33 on this box), and you can run whichever you want. The one
thing to take care of is .py associations; I haven't actually done it
(on here, all I actually do is IDLE, pretty much), but as of 3.3,you
should be able to use a Unix-style shebang to indicate which Python
you want to invoke.

ChrisA

I'm not even using shebangs in Windows because I thought it wouldn't make any difference. I'm used to run scripts like "python filename.py" from the command line. No problem!

So, Python 3.3 will honor if I insert "#!C:\Python27\python.exe"? if I install it after Python 2.7? Cool!!!
 
M

Mark Lawrence

Easy. Just grab the standard installer and hit it. You'll get two
separate directories (or more; I have \Python26, \Python27, \Python32,
\Python33 on this box), and you can run whichever you want. The one
thing to take care of is .py associations; I haven't actually done it
(on here, all I actually do is IDLE, pretty much), but as of 3.3, you
should be able to use a Unix-style shebang to indicate which Python
you want to invoke.

ChrisA

See this
http://docs.python.org/3/whatsnew/3.3.html#pep-397-python-launcher-for-windows
 
L

Lee Crocker

Why on Earth would you want to? "Cutting" a deck makes no sense in software.. Randomize the deck properly (Google "Fisher-Yates") and start dealing. Cutting the deck will not make it any more random, and in fact will probably make it worse depending on how you choose the cutpoint.

The purpose of "cutting" cards is to make it more difficult for human dealers to stack a deck. Simulating it in software makes no more sense than simulating the cigars you smoke while playing.
 
M

Modulok

Why on Earth would you want to? "Cutting" a deck makes no sense in
software. Randomize the deck properly (Google "Fisher-Yates") and start
dealing. Cutting the deck will not make it any more random, and in fact
will probably make it worse depending on how you choose the cutpoint.

The purpose of "cutting" cards is to make it more difficult for human
dealers to stack a deck. Simulating it in software makes no more sense than
simulating the cigars you smoke while playing.
Perhaps the OP wanted to study the efficiency and affect of a real-world
shuffling algorithm :p Maybe he was designing a probabilistic magic trick
and
needed to evaluate how a cut would modify the outcome of a particular stack.
Maybe it was a school assignment. Who knows?

(But yeah if the purpose was for pure randomization then there's no real
point.)

There could be a lot of legitimate reasons though.
-Modulok-
 
G

Giorgos Tzampanakis

Suppose I have a deck of cards, and I shuffle them

import random
cards = []
decks = 6
cards = list(range(13 * 4 * decks))
random.shuffle(cards)

So now I have an array of cards. I would like to cut these cards at some
random point (between 1 and 13 * 4 * decks - 1, moving the lower half of
that to the top half of the cards array.

For some reason, I can't see how this can be done (I know that it must
be a simple line or two in Python, but I am really stuck here). Anyone
have any direction they can give me on this? Thanks, RVic, python newbie

The slice notation should be your friend here:

random.shuffle(cards)
cut_point = random.choice(xrange(len(cards)))
cards = cards[cut_point :] + cards[: cut_point]
 
J

Joshua Landau

Why on Earth would you want to? "Cutting" a deck makes no sense in software. Randomize the deck properly (Google "Fisher-Yates") and start dealing. Cutting the deck will not make it any more random,
True

and in fact will probably make it worse depending on how you choose the cutpoint.

I'm pretty sure it won't. Otherwise you'd be lowering entropy by doing
a random thing to a random thing.
 
L

Lee Crocker

I'm pretty sure it won't. Otherwise you'd be lowering entropy by doing
a random thing to a random thing.

Doing a random thing to a random thing usually *does* lower entropy when
the "random" things are actually deterministic algorithms that may have unexpected correlations. That's why you don't write your own PRNG unless
you have a very good understanding of the math.

If you are shuffling the deck with, say, numbers from random.org (which uses atmospheric noise), then cutting the deck afterward will have precisely 0effect, since the (51 * 52!) possible outcomes include 51 copies of each of the 52! orderings, and so the odds of each end up the same. But if you'rechoosing the cutpoint by getting a value from the same PRNG you used to shuffle, there might very well be a correlation that makes some arrangements more likely
than others.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top