[Tutor] Having trouble with a component of the random module

Alan Gilfoy agilfoy at frontiernet.net
Thu Aug 28 08:33:19 CEST 2008


One of my for-fun Python products is a booster-pack generator for  
trading card games (specifically, Magic the Gathering, the one game of  
the genre that I play heavily)
Somewhat like baseball cards, the cards in a pack are a  
somewhat-random selection of cards from the set in question, broken  
down by rarity levels.

There are anywhere from two to six rarity levels/groupings per set;  
this depends on the set in question.
I have a function for 2-list sets, another for 3, another for 4,  
another for 5, and another for 6.

By lists I mean the following: If a set has, say, 2 rarity levels in  
it, I make 2 lists, one per rarity level. Each list is a text file  
that has the names of each card of that rarity level in that set, one  
per line. (There are no blank lines.)

My function is set up kind of like this: Import the lists - via  
open("filename.txt").readlines()
This action creates a List, with strings as the list elements, one  
string/element for each line of the imported text file

I use random.sample to make the selection, random.sample(population, k)

"
for card in random.sample(Common, C):
     print card
"

Common is the 1st list, with the names of the commons in the set as  
the items in that list. The C variable is an integer, the amount of  
commons in a booster pack of that set. Say, if there are 11 commons in  
a booster pack of that set, I feed 11 as the value for C, and the code  
snippet above proceeds to print 11 randomly-selected names from the  
Commons list

My program does the same thing for the second list (Uncommon), the  
third list (Rare), the fourth list (BasicLand), the fifth list  
(SpecialA), and the sixth list (SpecialB).

For card sets that use two, three, or four lists, the results are  
displayed just fine.

For card sets that use five or six lists, the results for the first  
four are displayed just fine, and then I get this:

The code that processes the 1st, 2nd, 3rd and 4th lists is very  
similar to the code that processes the 5th and 6th, so I don't see  
what's happening.

Traceback (most recent call last):
   File "C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\BoosterPackMaker.py", line  
104, in <module>
     SixList_PackChooser(C_list, UC_list, R_list, BL_list,  
SpecialA_list, SpecialB_list, C, UC, R, BL, SA, SB)
   File "C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\PackGenerator.py",  
line 367, in SixList_PackChooser
     for card in random.sample(SpecialA, SA):
   File "C:\Python25\lib\random.py", line 303, in sample
     raise ValueError, "sample larger than population"
ValueError: sample larger than population

Looking at the mentioned line in random.py, I don't see how my program  
is triggering the ValueError.

The number of items I want from a list is smaller than the population  
(number of items) in the list, so it should work.

In this specific case, I'm asking for one item from a five-item list.

Overall, more code is involved that I think can be represented in a  
few cut'n'paste snippets; how would I go about providing the files in  
question for interested parties to look at?
(There's the Python file for my front-end UI code, the Python module  
file for my processing function, and the relevant text files)

Apologizing for message length; I'm just trying to clearly explain the  
problem that I want assistance on.



More information about the Tutor mailing list