Shuffling lists
I''ve been looking over the Python documentation and decided to make my first script with python. I want to make a simple card game, so I figure I''ll create a list with all of the cards (A,K,Q,J, 10, Etc.). The thing is, I didn''t see anything in the documentation about mixing lists up so they''re in a random order? If anyone knows how to do that I''d appreciate it. I need to be able to shuffle my cards
Thanks,
One simple way to shuffle is to simply choose two cards at random in the deck and swap their positions. Keep doing this for some arbitrary number of times until you feel the deck is shuffled sufficiently.
Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller
Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller
I can think of the three fopllowing [EDIT : following] methods :
Method 1
Read the cards one at a time and add them to another list.
Before inserting a card, choose a random number N (between 1 and the number of cards already in the list plus 1). Insert the new card at the Nth position in the list.
Expected complexity for n cards : O(n²)
Method 2
Associate to each card a random integer, then sort these cards by their integer.
Expected complexity for n cards : O(n log n)
Method 3
Read the cards one at a time and add them to another list. Before inserting a card, "flip a coin", if tails then add it to the front of the list, if heads then add it to the back instead.
Expected complexity for n cards : O(n)
( You cannot get all possible permutations with this method )
[edited by - ToohrVyk on February 14, 2004 10:45:28 AM]
Method 1
Read the cards one at a time and add them to another list.
Before inserting a card, choose a random number N (between 1 and the number of cards already in the list plus 1). Insert the new card at the Nth position in the list.
Expected complexity for n cards : O(n²)
Method 2
Associate to each card a random integer, then sort these cards by their integer.
Expected complexity for n cards : O(n log n)
Method 3
Read the cards one at a time and add them to another list. Before inserting a card, "flip a coin", if tails then add it to the front of the list, if heads then add it to the back instead.
Expected complexity for n cards : O(n)
( You cannot get all possible permutations with this method )
Victor Nicollet, INT13 game programmer
[edited by - ToohrVyk on February 14, 2004 10:45:28 AM]
ASPN Cookbook recipe search results for "shuffle"
ASPN Cookbook, for future reference.
[Edit: fscked link. Grrr...]
[edited by - Oluseyi on February 14, 2004 12:41:41 PM]
ASPN Cookbook, for future reference.
[Edit: fscked link. Grrr...]
[edited by - Oluseyi on February 14, 2004 12:41:41 PM]
[OT alert]After I read the code on that web site... I have a newfound respect for pseudocode.
*sighs, shakes head, chuckles*
import random
random.shuffle(theList)
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
import random
random.shuffle(theList)
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement