Programming a Roman thumb
Don’t worry, I’ve got some more cool stuff with the random table management script for later. But sometimes you don’t want to have to set up even a simple table. You just want to make a quick choice. “Death or mercy?” asks the gladiator, and the emperor responds with?
- $ ./choose death mercy
As problems go, you can’t get much simpler than that. Here’s the code:
- #!/usr/bin/python
- import random, optparse
- parser = optparse.OptionParser()
- (options, args) = parser.parse_args()
- print random.choice(args)
Save it as “choose”. Make it executable using “chmod u+x choose” just as in the previous installment. You now have a script that lets you make a quick choice between any arbitrary possibilities.
- $ ./choose death mercy
- mercy
- $ ./choose mercury venus earth mars jupiter saturn uranus neptune pluto X
- uranus
And a tip: the Unix command line uses spaces to separate arguments and options. If you want a single argument to include a space, surround the entire argument with quotes.
- $ ./choose Greyhawk "Forgotten Realms"
- Forgotten Realms
Ooh, that hurt.
More Programming for Gamers
- Are my dice random?
- My d20 appears to have been rolling a lot of ones, a disaster if I were playing D&D but a boon for Gods & Monsters. Is my die really random, or is it skewed towards a particular result? Use the ‘R’ open source statistics tool to find out.
- Programming for Gamers: Choosing a random item
- If you can understand a roleplaying game’s rules, you can understand programming. Programming is a lot easier.
- Easier random tables
- Rather than having to type --table and --count, why not just type the table name and an optional count number?
- Multiple tables on the same command
- The way the “random” script currently stands, it does one table at a time. Often, however, you have more than one table you know you’re going to need. Why not use one command to rule them all?
- How random is Python’s random?
- Can you trust a script’s random number generator to actually be random?
- 12 more pages with the topic Programming for Gamers, and other related pages