I'm looking to create a script in python that will generate 5 random cards from a preset list of 1000 without any of the cards repeating in the same deck, this would include the art for the card and a small text box saying the name and bio of the card. I found some help online about creating a card randomizer but I don't know how to change it to select several cards from a series
-
1If you have a list of elements, you could use `random.choice` from the [standard library](https://docs.python.org/3/library/random.html#functions-for-sequences) to select 5 at random. If your question is about how to show “art” for the card or store other attributes, you’re better off writing a separate question about how to represent the cards and deck. – NicholasM Aug 01 '20 at 03:41
-
1Share the code you've already tried and you may like to edit the title of your question as well – Mrinal Roy Aug 01 '20 at 03:42
-
1@NicholasM - random.choice() uses replacement. You need to use random.sample() in this case. – hrokr Aug 01 '20 at 04:02
-
StackOverflow is not a coding service. Please read the following documentation, then [edit] and rephrase the question. [Take the Tour](https://stackoverflow.com/tour) & [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Always [Provide a Minimal, Reproducible Example (e.g. code, data, errors) as text](https://stackoverflow.com/help/minimal-reproducible-example) & you're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Trenton McKinney Aug 01 '20 at 04:14
1 Answers
First of all, you don't want to use a list in this case due to lookup times. The lookup will O(n). Meaning if you had a super duper slow computer that could look at one item per .1 sec, it could take as long as 100 sec.
Instead, you want to use a dictionary in the case as lookups are O(1), meaning that same super duper slow computer would take .1 sec to look up the image. So much faster. It also happens that a dict will allow you to have the card, the write-up and the image (as in badman.jpeg, not the actual photo).
The thing about dict is they use a key/value pair and you seem to need something more like key with two values. Not to worry, but a little more thought is useful. Let's say you didn't want to let the card to be modifiable. The image isn't the card title isn't and the write-up isn't. In this case, a tuple would be useful. So you would have a key, with a value of a tuple.
There are a number of ways to go from here. You could have the key be the name of the card as presumably, it's unique. A dict constructor could be used too. You could also just enumerate them.
In this case, you dict might look somethign like cards = {'1':(name, desc, name.jpg), '2':(name2, desc2, name2.jpg), '3':(name3, desc3, name3.jpg)}
Second, you want to use random.sample() else you run the risk of drawing the same card twice which is what you said you didn't want to use.
As for how to use random.sample() with a dict, let me refer you to another answer on that topic.

- 3,276
- 3
- 21
- 39
-
thanks a ton man, i needed to figure this out pretty quick here and this was so helpful – Hellenden Gamer Aug 01 '20 at 22:07