Generating Random Numbers


As much as users want consistent, predictable results from programs, sometimes what makes the programs exciting is their unpredictability: the sudden change in a computer opponent's strategy, or an alien creature bursting out from an arbitrary door. Random numbers can supply this element of chance or surprise, and Python provides an easy way to generate those random numbers.

TRAP

Python generates random numbers based on a formula, so they are not truly random. This kind of random generation is called pseudorandom and is good enough for most applications (just don't try to start an online casino with it). If you really need truly random numbers, visit http://www.fourmilab.ch/hotbits/. The site generates random numbers based on the natural and unpredictable process of radioactive decay.

Introducing the Craps Roller Program

Craps Roller replicates the dice roll of the fast-paced, casino game of craps. But you don't have to know anything about craps to appreciate the program. Craps Roller just simulates the roll of two, six-sided dice. It displays the value of each and their total. To determine the dice values, the program uses a function that generates random numbers. Figure 3.2 shows the program in action.

click to expand
Figure 3.2: Ack! I got a total of 7 on my first roll, which means I lose.

Here's the code:

 # Craps Roller # Demonstrates random number generation # Michael Dawson - 12/29/02 import random # generate random numbers 1 - 6 die1 = random.randrange(6) + 1 die2 = random.randrange(6) + 1 total = die1 + die2 print "You rolled a", die1, "and a", die2, "for a total of", total raw_input("\n\nPress the enter key to exit.") 

Using the import Statement

The first line of code in the program introduces the import statement. The statement allows you to import, or load, modules, in this case the random module in:

 import random 

Modules are files that contain code meant to be used in other programs. These modules usually group together a collection of programming related to one area. The random module contains functions related to generating random numbers and producing random results.

If you think of your program as a construction project, then modules are like special toolkits that you can pull out from the garage when you need them. But instead of going to the shelf and grabbing a powered, circular saw, here, I imported the random module.

Once you import a module, you can use its code. Then, it just becomes a matter of accessing it.

Accessing randrange()

The random module contains a function, randrange(), which produces a random integer. The Craps Roller program accesses randrange() through the following function call:

 random.randrange(6) 

You'll notice the program doesn't directly call randrange(). Instead, it's called with random.randrange(), because the program accesses randrange() through its module, random. In general, you can call a function from an imported module by giving the module name, followed by a period, followed by the function call itself. This method of access is called dot notation. Dot notation is like the possessive in English. In English, "Mike's Ferrari" means that it's the Ferrari that belongs to Mike. Using dot notation, random.randrange() means the function randrange() that belongs to the module random. Dot notation can be used to access different elements of imported modules.

Now that you know how to access randrange(), you need to know how to use it.

Using randrange()

There are several ways to call randrange(), but the simplest is to use a single, positive, integer argument. Called this way, the function returns a random integer from, and including, 0, up to, but not including, that number. So the call random.randrange(6) produces either a 0, 1, 2, 3, 4, or 5. Alright, where's the 6? Well, randrange() is picking a random number from a group of six numbers—and the list of numbers starts with 0. You may think this is odd, but you'll find that most computer languages start counting at 0 instead of 1. So, I just added 1 to the result to get the right values for a die:

 die1 = random.randrange(6) + 1 

Now, die1 gets either a 1, 2, 3, 4, 5, or 6.

TRAP

It's a common mistake to think that the single argument you provide randrange() could be returned as a result. It can't. Remember, randrange() starts counting at 0, so you'll get back a random number between (and including) 0 and up to one less than the number you provide.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net